Home > Enterprise >  Operate inside a list of data frames in R
Operate inside a list of data frames in R

Time:07-01

I created a list with

list.files(pattern="*.csv")
file_list <- lapply(list_of_files, read_csv, col_names = TRUE)

Do you know can I apply the same function to all the files in the list? I tried with function(df) but it doesn't work. I need to:

  • Rename all the columns inside the data frames in the same way
  • Concatenate column A and B, and C and D inside of each data frames.

Thank you.

CodePudding user response:

library(dplyr)

file_list <- lapply(file_list, function(x) {
  setNames(x, c("A", "B", "C", "D")) %>%
    mutate(AB = paste0(A, B), CD = paste0(C, D)) %>%
    return()
})

CodePudding user response:

Here is an example of just doing all in the original lapply, as I mentioned in the comments

library(dplyr)
library(readr)

lapply(list_of_files, \(f) {
  read_csv(f, col_names=TRUE) %>% 
    rename_with(~c("A", "B", "C", "D")) %>%  
    mutate(newcol1 = paste0(A,B),newcol2 = paste0(C,D))
})

CodePudding user response:

Your lapply(list_of_files, read_csv, col_names = TRUE) runs functions read_csv(file=list_of_files[[1]], colnames=TRUE), read_csv(file=list_of_files[[2]], colnames=TRUE) etc. and returns output from each run as a slot of a list.

It can run any function. You can either define your own function like

my_data_trans<-function(fn){
df<- read_csv(fn, colnames=TRUE)
colnames(df)<- c("Number", "Charge", "Potential_Ch", "Discharge","Potential_Disch")) 
dfcon <- rbind(df[,c(1, 2,4)], df[,c(1, 3, 5)])
colnames(df)<-c("Number", "Charge Discharge", "Potential_Ch Potential_Disch")) 
# any other transformation
return(dfcon)
}
lapply(list_of_files, my_data_trans)

Or you can use anonymous function = skip name and put only function definition

lapply(list_of_files, function(fn) {
#same as above
})
  • Related