Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a "1" to end of each variable name in R?

I have a dataframe "data" with 50 variables. For the analysis purpose, I want to rename all these variables by adding 1 at the end of each variable . Following is the procedure that I followed (for a dataframe "datasample" of 10 variables):

names(datasample)
# original colnames for 10 variables
names(datasample)
  [1] "a"    "z"   "y"  "b"  "bb" "ca"   "a3"   
  [8] "b2" "as" "ask"
#rename 10 variables
names(datasample)<-c("a1","z1","y1","b1","bb1","ca1","a31","b21","as1","ask1")

I was wondering whether there is an efficient way of renaming these multiple variables. Thanks in advance.

like image 735
Metrics Avatar asked Sep 17 '25 01:09

Metrics


1 Answers

names(datasample) <- paste(names(datasample), "1", sep="")

Or, equivalently,

names(datasample) <- paste0(names(datasample), "1")
like image 151
Josh O'Brien Avatar answered Sep 19 '25 16:09

Josh O'Brien