I have dozens of csv files/dataframes in a folder. Many dataframes have unique column names, and column names are out of order. I need to reorganize the column names, and eventually merge all dataframes. First, I want to create a list of all unique column names among all data frames. I can do it manually:
setwd(workingDir)
DRP003667 <- read.csv('DRP003667.csv')
DRP003669 <- read.csv('DRP003669.csv')
DRP003667_ls <- colnames(DRP003667)
DRP003669_ls <- colnames(DRP003669)
myls <- unique(c(DRP003667_ls,DRP003669_ls))
myls
[1] "ID" "Time" "Date" "Length" "Width"
How can I make this more efficient?
How do I open all csv files from a folder? How do I pull the column names from all files?
CodePudding user response:
We may get the datasets in a list
from the object names created in the global environment using mget
and use lapply
to loop over the list, extract the column names, unlist
the list
and get the unique
unique(unlist(lapply(mget(ls(pattern = "^DRP\\d ")), names))