这是我使用的代码:
library(data.table)
# Get a List of all files in directory named with a key word, say all `.csv` files
filenames <- list.files("D:/FILES/yahoo cvs", pattern="*.csv", full.names=TRUE)
# read and row bind all data sets
data <- rbindlist(lapply(filenames,fread))
它工作得很好,但我想将文件与 cbindlist 合并。我只是简单地将 rbindlist 更改为 cbindlist,但它不起作用。我该怎么做?
File AAA
C1 C2
R1 10 20
R2 30 40
File BBB
C1 C2
R1 50 60
R2 80 70
Combined new file
AAA.C1 AAA.C2 BBB.C1 BBB.C2
R1 10 20 R1 50 60
R2 30 40 R2 80 70
而不是lapply
如果您将sapply
与simplify = FALSE
一起使用,它会在您cbind
时将文件名附加到列名中。
data <- do.call(cbind, sapply(filenames,data.table::fread, simplify = FALSE))