How do I change column names in list of data frames inside a function?

我怕爱的太早我们不能终老 提交于 2021-02-16 18:51:25

问题


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))

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)
}

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.


回答1:


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')

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

lapply(mr1, renameFunction, someNames) 

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



来源:https://stackoverflow.com/questions/42403272/how-do-i-change-column-names-in-list-of-data-frames-inside-a-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!