简体   繁体   English

使用lapply更改数据帧列表列表中的列名

[英]Change column names in list of list of data frames using lapply

This is a follow-up to this question: Create scatter plot with interval data in R 这是该问题的后续内容: 使用R中的间隔数据创建散点图

I would like to change the column names in the following data.frames, that are part of a list of lists: 我想更改以下data.frames中的列名,它们是列表列表的一部分:

other_list #a list of arbitrary length containing some data
myvar <- "myactualMeasurement"

lapply_output <- list()
for(i in 1:length(other_list)){
  lapply_output[[i]] <- lapply(other_list[[i]], function(item){
      out_df <- data.frame('MyItem' = item$MyItem,
                           'Measurement' = item$Measurement,
                           'Interval' = seq(floor(item$First), floor(item$Last))+ 0.5)
      return(out_df)
  })
}

As you can see, I'm assigning the names 'MyItem', 'Measurement' and 'Interval' to my columns. 如您所见,我将名称“ MyItem”,“ Measurement”和“ Interval”分配给我的列。 I would like to assign the name 'Measurement' using the variable "myvar" instead of doing it manually. 我想使用变量“ myvar”分配名称“ Measurement”,而不是手动执行。 I've already tried to use 我已经尝试使用

eval(parse(text = myvar))

instead of 'Measurement' in my lapply structure, but that does not seem to work. 而不是我笨拙的结构中的“测量”,但这似乎不起作用。

My current workaround is a nested loop which (re-)assigns the column name: 我当前的解决方法是一个嵌套循环,它(重新)分配列名:

for(i in 1:length(other_list)){
  for(j in 1:length(lapply_output[[i]])){
    colnames(lapply_output[[i]][[j]])[which(names(lapply_output[[i]][[j]]) == "Measurement")] <- myvarpar
  }
}

I'm sure, there has to be a more neat way of doing this (preferentially a oneliner in the lapply structure, but I can't come up with a good solution. 我敢肯定,必须有一种更简洁的方法来完成此操作(最好是在lapply结构中使用一个内衬,但是我无法提出一个好的解决方案。

An alternative could be (see Using lapply to change column names of a list of data frames ): 一种替代方法是(请参阅使用lapply更改数据帧列表的列名 ):

new_col_name <- c("MyItem", myvar, "Interval")
for(i in 1:length(other_list)){
  newlist[[i]] <- lapply(lapply_output[[i]], setNames, nm = new_col_name)
}

But this is 1) not really doing what it should do (only the last list element is preserved) 2) is also not neat 但这是1)没有真正做应做的事情(仅保留了最后一个list元素)2)也不整洁

Preferentially, I would like to use something like 优选地,我想使用类似

eval(parse(text = myvar))

in the original structure, without having to write much more additional naming code. 在原始结构中,而不必编写更多的其他命名代码。

By default lapply loops through the input list elements hence you need not duplicate with for loop indexing. 默认情况下, lapply循环遍历输入列表元素,因此您无需使用for循环索引进行重复。 Also there is no need to create a dummy list prior to lapply since default output class of lapply is a list object 另外,由于lapply的默认输出类是list对象,因此lapply在lapply之前创建虚拟列表。

You can rename the column name in one step as below with match being used to compare column names 您可以按照以下步骤一步一步重命名列名,并使用match来比较列名

outputVar <- "myactualMeasurement"
inputVar <- "Measurement"

outList = lapply(other_list, function(item){

      out_df <- data.frame('MyItem' = item$MyItem,
                           'Measurement' = item$Measurement,
                           'Interval' = seq(floor(item$First), floor(item$Last))+ 0.5)

      inputvarIndex <- match(inputVar,colnames(out_df))
      colnames(out_df)[inputvarIndex] <- outputVar

      return(out_df)
  })

I strongly suggest to thoroughly read the documentation and examples of ?lapply and note that eval/parse though seemingly convenient are vulnerable to unexpected results 我强烈建议您仔细阅读?lapply的文档和示例,并注意,尽管eval/parse看起来很方便,但很容易出现意外结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM