I have all of my .csv files in one folder. Each file contains the same columns, however, some of the files vary in the order (e.g., File 1 could have COLUMN A, COLUMN B, COLUMN C as the first three columns and File 2 could have COLUMN A, COLUMN C, COLUMN B as the first three columns). I could manually reorder each .csv file but I will have over 200 .csv files by the time I am finished and there are 142 columns in each .csv file.
Here is the code for what I have so far:
fileList <- list.files(path = "/path/to/folder/here",
recursive = TRUE,
pattern = "\\.csv$",
full.names = TRUE)
files <- readr::read_csv(fileList, show_col_types = FALSE)
This is the error I get:
Error: Files must have consistent column names:
* File 1 column 64 is: mrtRespPrac.rt
* File 2 column 64 is: mrtRespPrac.started
I get what the error is telling me so I am wondering if there is a way to reorder the columns in the files stored in fileList
so that they are the exact same order and then run the read_csv
line of code. I've tried all kinds of Googling but can't find any specific code to do what I want.
CodePudding user response:
Try
alldat <- lapply(fileList, read_csv, show_col_types = FALSE)
nms <- names(alldat[[1]])
combined <- do.call(rbind, lapply(alldat, subset, select = nms))