为什么我不能重命名列表中的数据框列?

时间:2021-10-04 23:12:42

I would like to rename some columns from CPU_Usage to the process name before I merge the dataframes in order to make it more legible.

我想在合并数据帧之前将CPU_Usage中的一些列重命名为进程名称,以使其更易读。

names(byProcess[[1]])
# [1] "Time"      "CPU_Usage"
names(byProcess[1])
# [1] "CcmExec_3344"
names(byProcess[[1]][2]) <- names(byProcess[1])
names(byProcess[[1]][2])
# [1] "CPU_Usage"
names(byProcess[[1]][2]) <- 'test'
names(byProcess[[1]][2])
# [1] "CPU_Usage"
lapply(byProcess, names)
# $CcmExec_3344
# [1] "Time"      "CPU_Usage"
#
# ... (removed several entries to make it more readable)
#
# $wrapper_1604
# [1] "Time"      "CPU_Usage"

1 个解决方案

#1


5  

names(l[[1]][2]) returns the names of the object which is l[[1]][2]. In your case this will be a data.frame with one column (CPU_Usage) using names<- will replace the names on this new object (and not replace l[[1]])

names(l [[1]] [2])返回对象的名称,即l [[1]] [2]。在你的情况下,这将是一个data.frame,其中一列(CPU_Usage)使用名称< - 将替换此新对象上的名称(而不是替换l [[1]])

if you want to use names<- on l[[1]], you will need to have this as the argument to names<-

如果你想在l [[1]]上使用名字< - ,你需要将它作为名字的参数< -

so

names(byProcess[[1])[2] <- names(byProcess[1])

performs the action you want

执行你想要的动作

#1


5  

names(l[[1]][2]) returns the names of the object which is l[[1]][2]. In your case this will be a data.frame with one column (CPU_Usage) using names<- will replace the names on this new object (and not replace l[[1]])

names(l [[1]] [2])返回对象的名称,即l [[1]] [2]。在你的情况下,这将是一个data.frame,其中一列(CPU_Usage)使用名称< - 将替换此新对象上的名称(而不是替换l [[1]])

if you want to use names<- on l[[1]], you will need to have this as the argument to names<-

如果你想在l [[1]]上使用名字< - ,你需要将它作为名字的参数< -

so

names(byProcess[[1])[2] <- names(byProcess[1])

performs the action you want

执行你想要的动作