Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在数据框中有 5 列,我想将变量名称重命名为 Var1、Var2、Var3、Var4、Var5。
我目前的做法不是很方便:
names(mydata)[1:10] <- c("Var1", "Var2", "Var3", "Var4", "Var5")
有没有更好的方法,当我有 100 个变量时,我不必输入所有 100 个名称?
names(mydata)[1:100] <- paste("Var", 1:100, sep="")
该sprintf功能将非常适合:
sprintf
names(mydata)[1:100] <- sprintf("Var%d", 1:100)
或者
names(mydata)[1:100] <- sprintf("Var%03d", 1:100)
取决于您是否想要“Var1”、“Var2”、... 或“Var001”、“Var002”、...等名称