如何在R中的每个变量名的末尾添加“1”?

时间:2012-12-31 02:59:49

标签: r variables rename

我有一个包含50个变量的数据框“数据”。出于分析目的,我想通过在每个变量的末尾添加1来重命名所有这些变量。以下是我遵循的程序(对于10个变量的数据帧“数据样本”):

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")

我想知道是否有一种有效的方法来重命名这些多个变量。提前致谢。

1 个答案:

答案 0 :(得分:3)

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

或者,相当于

names(datasample) <- paste0(names(datasample), "1")