要从列名中删除通用后缀,我们可以使用gsub函数。例如,如果我们有一个数据帧df,其中包含定义为x1df,x2df,x3df和x4df的列,则可以使用以下命令从所有列名称中删除df:
colnames(df)<-gsub("df","",colnames(df))
请看以下数据帧:
> x1Data<-rnorm(20,25,4) > x2Data<-rnorm(20,25,1.2) > x3Data<-runif(20,2,5) > df1<-data.frame(x1Data,x2Data,x3Data) > df1
输出结果
x1Data x2Data x3Data 1 29.26500 26.64124 2.598983 2 21.82170 23.41442 4.134393 3 22.71918 25.21586 4.442823 4 19.88633 25.23487 3.338448 5 20.48989 23.33683 3.829757 6 29.07910 25.54084 3.519393 7 24.28573 23.67258 4.667397 8 27.99849 22.97148 4.100405 9 23.48148 25.36574 2.618030 10 26.39401 23.80191 4.235092 11 29.39867 24.36261 2.782559 12 30.11137 24.62702 4.873779 13 23.56623 25.29017 2.255684 14 24.18464 25.59862 2.247147 15 23.35541 25.38190 4.704027 16 25.02549 25.76776 4.706971 17 18.24187 23.53798 2.411423 18 24.12003 25.27751 2.137409 19 27.58055 24.80092 3.992380 20 23.70832 25.73701 2.577801
从数据框df1的列名中删除数据一词:
> colnames(df1)<-gsub("Data","",colnames(df1)) > df1
输出结果
x1 x2 x3 1 29.26500 26.64124 2.598983 2 21.82170 23.41442 4.134393 3 22.71918 25.21586 4.442823 4 19.88633 25.23487 3.338448 5 20.48989 23.33683 3.829757 6 29.07910 25.54084 3.519393 7 24.28573 23.67258 4.667397 8 27.99849 22.97148 4.100405 9 23.48148 25.36574 2.618030 10 26.39401 23.80191 4.235092 11 29.39867 24.36261 2.782559 12 30.11137 24.62702 4.873779 13 23.56623 25.29017 2.255684 14 24.18464 25.59862 2.247147 15 23.35541 25.38190 4.704027 16 25.02549 25.76776 4.706971 17 18.24187 23.53798 2.411423 18 24.12003 25.27751 2.137409 19 27.58055 24.80092 3.992380 20 23.70832 25.73701 2.577801
让我们看另一个例子:
> a_treatment<-rpois(20,5) > b_treatment<-rpois(20,10) > c_treatment<-rpois(20,2) > d_treatment<-rpois(20,8) > df2<-data.frame(a_treatment,b_treatment,c_treatment,d_treatment) > df2
输出结果
a_treatment b_treatment c_treatment d_treatment 1 3 18 3 4 2 1 9 1 5 3 3 13 0 5 4 2 14 0 9 5 4 9 1 10 6 6 8 0 8 7 4 7 5 9 8 2 13 1 8 9 5 4 4 7 10 6 11 1 7 11 4 9 3 12 12 7 6 4 10 13 6 20 3 6 14 4 10 1 4 15 13 11 0 12 16 6 11 1 10 17 6 8 1 16 18 4 8 1 14 19 8 11 2 8 20 3 7 0 9
从数据框df2的列名中删除单词_treatment:
> colnames(df2)<-gsub("_treatment","",colnames(df2)) > df2
输出结果
a b c d 1 3 18 3 4 2 1 9 1 5 3 3 13 0 5 4 2 14 0 9 5 4 9 1 10 6 6 8 0 8 7 4 7 5 9 8 2 13 1 8 9 5 4 4 7 10 6 11 1 7 11 4 9 3 12 12 7 6 4 10 13 6 20 3 6 14 4 10 1 4 15 13 11 0 12 16 6 11 1 10 17 6 8 1 16 18 4 8 1 14 19 8 11 2 8 20 3 7 0 9