如何在函数内的数据帧列表中更改列名? [英] How do I change column names in list of data frames inside a function?

查看:30
本文介绍了如何在函数内的数据帧列表中更改列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何更改数据帧列表中的名称"的答案已被多次回答.但是,我一直试图生成一个可以将任何列表作为参数并更改列表中所有数据框的所有列名称的函数.我正在使用大量.csv文件,所有这些文件都具有相同的3列名.我正在按组导入文件,如下所示:

I know that the answer to "how to change names in a list of data frames" has been answered multiple times. However, I'm stuck trying to generate a function that can take any list as an argument and change all of the column names of all of the data frames in the list. I am working with a large number of .csv files, all of which will have the same 3 column names. I'm importing the files in groups as follows:

# Get a group of drying data data files, remove 1st column
files <- list.files('Mang_Run1', pattern = '*.csv', full = TRUE)
mr1 <- lapply(files, read.csv, skip = 1, header = TRUE, colClasses = c("NULL", NA, NA, NA))

我将有6个这样的文件组.如果我在单个列表上运行以下代码,则指定列表内每个数据框中的列名将正确更改.

I will have 6 such file groups. If I run the following code on a single list, the names of the columns in each data frame within the specified list will be changed correctly.

for (i in seq_along(mr1)) {
  names(mr1[[i]]) <- c('Date_Time', 'Temp_F', 'RH')
}

但是,如果我尝试推广该函数(请参见下面的代码)以将任何列表作为参数,则它将无法正常工作.

However, if I try to generalize the function (see code below) to take any list as an argument, it does not work correctly.

nameChange <- function(ls) {
  for (i in seq_along(ls)) {
    names(ls[[i]]) <- c('Date_Time', 'Temp_F', 'RH')
  }
  return(ls)
}

当我在MR1上调用 nameChange 时(从上面生成的列表),它会将列表的全部内容打印到控制台,并且不会更改列表内数据框中的列名称..在这里,我显然遗漏了一些有关R的内部原理的基本知识.我已经尝试过使用带有或不带有 return 的上述功能,并且对代码进行了多次修改,但都没有一个被证明是成功的.我非常感谢您的帮助,也非常想了解问题背后的原因".在过去处理以列表为参数的函数时,我遇到了很多麻烦.

When I call nameChange on mr1 (list generated from above), it prints the entire contents of the list to the console and does not change the names of the columns in the data frames within the list. I'm clearly missing something fundamental about the inner workings of R here. I've tried the above function with and without return, and have made several modifications to the code, none of which have proven successful. I'd greatly appreciate any help, and would really like to understand the 'why' behind the problem as well. I've had considerable trouble in the past handling functions that take lists as arguments.

非常感谢任何建设性的投入.

Thanks very much in advance for any constructive input.

推荐答案

认为这可能是一个非常简单的解决方法:首先,概括用于重命名列的函数.只需一次在一个数据帧上工作.

I think this might be a very simple fix: First, generalize the function you are using to rename the columns. This only needs to work on one dataframe at a time.

 renameFunction<-function(x,someNames){
      names(x) <- someNames
      return(x)
     }

现在,我们需要定义要将每个列名称更改为的名称.

Now we need to define the names we want to change each column name to.

someNames <- c('Date_Time', 'Temp_F', 'RH')

然后,我们调用新函数并将其应用于"mr1"列表中的每个元素.

Then we call the new function and apply it to every element of the "mr1" list.

lapply(mr1, renameFunction, someNames) 

关于您的确切位置,我可能弄错了一些细节,但是在解决类似问题之前,我已经使用过这种方法.由于您可以在特定情况下使用它,因此,我敢肯定,使用 lapply

I may have gotten some of the details wrong with regards to your exact sitiuation, but I've used this method before to solve similar issues. Since you were able to get it to work on the specific case, I'm pretty sure this will generalize readily using lapply

这篇关于如何在函数内的数据帧列表中更改列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆