更改数据框中某些列的名称[重复]
Posted
技术标签:
【中文标题】更改数据框中某些列的名称[重复]【英文标题】:change the names for certain columns in a data frame [duplicate] 【发布时间】:2016-06-08 13:20:14 【问题描述】:如果我想将名称从 2 列更改为末尾,为什么我的命令不起作用?
fredTable <- structure(list(Symbol = structure(c(3L, 1L, 4L, 2L, 5L), .Label = c("CASACBM027SBOG",
"FRPACBW027SBOG", "TLAACBM027SBOG", "TOTBKCR", "USNIM"), class = "factor"),
Name = structure(1:5, .Label = c("bankAssets", "bankCash",
"bankCredWk", "bankFFRRPWk", "bankIntMargQtr"), class = "factor"),
Category = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Banks", class = "factor"),
Country = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "USA", class = "factor"),
Lead = structure(c(1L, 1L, 3L, 3L, 2L), .Label = c("Monthly",
"Quarterly", "Weekly"), class = "factor"), Freq = structure(c(2L,
1L, 3L, 3L, 4L), .Label = c("1947-01-01", "1973-01-01", "1973-01-03",
"1984-01-01"), class = "factor"), Start = structure(c(1L,
1L, 1L, 1L, 1L), .Label = "Current", class = "factor"), End = c(TRUE,
TRUE, TRUE, TRUE, FALSE), SeasAdj = c(FALSE, FALSE, FALSE,
FALSE, TRUE), Percent = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Fed", class = "factor"),
Source = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Res", class = "factor"),
Series = structure(c(1L, 1L, 1L, 1L, 2L), .Label = c("Level",
"Ratio"), class = "factor")), .Names = c("Symbol", "Name",
"Category", "Country", "Lead", "Freq", "Start", "End", "SeasAdj",
"Percent", "Source", "Series"), row.names = c("1", "2", "3",
"4", "5"), class = "data.frame")
然后为了将第二列名称更改为末尾,我使用以下命令但不起作用
names(fredTable[,-1]) = paste("case", 1:ncol(fredTable[,-1]), sep = "")
或
names(fredTable)[,-1] = paste("case", 1:ncol(fredTable)[,-1], sep = "")
一般来说,例如如何更改特定列的列名 2 到结尾,2 到 7 等等,并将其设置为他/她喜欢的名字
【问题讨论】:
子集在函数外部而不是函数内部。如names(x)[-1]
而不是names(x[-1])
。更多信息请点击链接***.com/questions/6081439/…
@Pierre Lafortune 你能给我一个完整的答案吗?我试过了,我得到错误 Error in 1:ncol(fredTable)[-1] : argument of length 0 。然后我尝试了 names(fredTable)[-1] = paste("case", 1:ncol(fredTable), sep = "") 并再次收到错误警告消息: In names(dfm)[-1] = paste("Fraction", 1:ncol(dfm), sep = "") : 要替换的项目数不是替换长度的倍数
【参考方案1】:
通过在函数外部设置子集来替换特定列名,而不是像第一次尝试那样在 names
函数内:
> names(fredTable)[-1] <- paste("case", 1:ncol(fredTable[,-1]), sep = "")
说明
如果我们将新名称保存在向量 newnames
中,我们可以使用替换函数调查幕后发生的事情。
#These are the names that will replace the old names
newnames <- paste("case", 1:ncol(fredTable[,-1]), sep = "")
我们应该始终用以下格式替换特定的列名:
#The right way to replace the second name only
names(df)[2] <- "newvalue"
#The wrong way
names(df[2]) <- "newvalue"
问题是您试图创建一个新的列名向量,然后将输出分配给数据框。这两个操作在正确的替换中同时完成。
正确的方式[内部]
我们可以扩展函数调用:
#We enter this:
names(fredTable)[-1] <- newnames
#This is carried out on the inside
`names<-`(fredTable, `[<-`(names(fredTable), -1, newnames))
错误的方式[内部]
换错方式的内部是这样的:
#Wrong way
names(fredTable[-1]) <- newnames
#Wrong way Internal
`names<-`(fredTable[-1], newnames)
请注意,没有`[<-`
分配。子集数据框 fredTable[-1]
不存在于全局环境中,因此不会发生对 `names<-`
的分配。
【讨论】:
太棒了!当我们想要选择部分列名(如第 2 到 7 列)时,您是否也可以这样做。这样,我们涵盖了所有类型的列名更改,以后可以与其他用户一起使用。我喜欢你的回答,我接受了。所以请更改它以使其更通用以上是关于更改数据框中某些列的名称[重复]的主要内容,如果未能解决你的问题,请参考以下文章