仅更改特定列名称

时间:2014-07-16 11:59:57

标签: r dataframe

我读了一个包含以下列名的.csv文件:

mycolnames <- c("ID", "RECORDE.CODE" ,"X...." ,"X.....1", "X.....2" ,"X.....3", "Q14", "Q15.16", "Q17.18", "Q19",
"Q20" ,"Q21...." ,"Q22....", "Q23" ,"Q24", "Q25" ,"Q26", "Q27", "Q28" ,"Q29",
"Q30" ,"Q31" ,"Q32", "Q33" ,"Q34" ,"Q35" ,"Q36", "Q37", "Q38", "Q39",
"Q40" ,"Q41" ,"Q42" ,"Q43", "Q44", "Q45" ,"Q46", "Q47", "Q48" ,"Q48.1",
"Q50" ,"Q51", "Q52" ,"Q53.73" ,"X......A", "X......B", "Q21","Q22.A" ,"Q22.B")

我想仅为特定列更改名称(所有列都不以Q开头)。 我可以逐个重命名它们,但我无法在一个(长)命令中设置它们。

很想知道这一点。 感谢

1 个答案:

答案 0 :(得分:4)

如果data是您的数据集且newnames是新名称矢量,那么此类内容应该有效

colnames(data)[!grepl("^Q", colnames(data))] <- newnames