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

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

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

最佳答案

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

或者,等效地,
names(datasample) <- paste0(names(datasample), "1")

10-08 07:31