Reputation: 161
I have a data frame with N number of columns. In this case 25 and would like to change the suffix only from colum variables 15 to 30.
t0 is the dataframe with the 30 column variables
For all the variable 1 to 30, the following command works perfect:
t0<-data.frame(a=c(1),b=c(1),c=c(1),d=c(1),e=c(1),f=c(1),g=c(1),h=c(1))
colnames(t0) <- paste( colnames(t0), "Sub",sep = "_")
names(t0)
[1] "a_Sub" "b_Sub" "c_Sub" "d_Sub" "e_Sub"
[6] "f_Sub" "g_Sub" "h_Sub" "i_Sub" "ii_Sub"
[15] "j_Sub" "k_Sub" "l_Sub" "m_Sub" "n_Sub"
Desired output:
names(t0)
[1] "a" "b" "c" "d" "e"
[6] "f" "g" "h" "i" "ii"
[15] "j_Sub" "k_Sub" "l_Sub" "m_Sub" "n_Sub"
Any idea how to get this done in R?
Thanks,
Albit
Upvotes: 1
Views: 312
Reputation: 887158
The reason why it didn't work was due to subsetting the dataset and then get the column names. Instead, we can directly get the column names of the entire dataset and subset the columns with numeric index
colnames(t0)[15:30] <- paste(colnames(t0)[15:30], "Sub", sep="_")
Upvotes: 2