我將所有 .csv 檔案放在一個檔案夾中。每個檔案包含相同的列,但是,一些檔案的順序不同(例如,檔案 1 的前三列可能有 COLUMN A、COLUMN B、COLUMN C,而檔案 2 可能有 COLUMN A、COLUMN C、COLUMN B作為前三列)。我可以手動重新排序每個 .csv 檔案,但到我完成時我將擁有 200 多個 .csv 檔案,并且每個 .csv 檔案中有 142 列。
這是我到目前為止的代碼:
fileList <- list.files(path = "/path/to/folder/here",
recursive = TRUE,
pattern = "\\.csv$",
full.names = TRUE)
files <- readr::read_csv(fileList, show_col_types = FALSE)
這是我得到的錯誤:
Error: Files must have consistent column names:
* File 1 column 64 is: mrtRespPrac.rt
* File 2 column 64 is: mrtRespPrac.started
我得到了錯誤告訴我的資訊,所以我想知道是否有辦法對存盤在其中的檔案中的列進行重新排序,fileList使它們的順序完全相同,然后運行read_csv代碼行。我嘗試了各種谷歌搜索,但找不到任何特定的代碼來做我想做的事。
uj5u.com熱心網友回復:
嘗試
alldat <- lapply(fileList, read_csv, show_col_types = FALSE)
nms <- names(alldat[[1]])
combined <- do.call(rbind, lapply(alldat, subset, select = nms))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/529513.html
標籤:rCSV进口
