我正在尝试在一个df中保存多个csv文件,并在df中包含一个带有文件日期的新列。我已经读取了所有的文件来获得一个df,但是我不能为每个文件添加date列。我正在使用下面的代码
ccn_files <- list.files(pattern = '*.csv', path = "input/CCN/") ##Creates a list of all the files
ccn_data_raw <- do.call("rbind", ##Apply the bind to the files
lapply(ccn_files, ##call the list
function(x) ##apply the next function
read.csv(paste("input/CCN/", x, sep=''),fill = T, header = TRUE,
skip = 4)))我还可以使用下面这行代码从矢量中的所有文件中获取日期
test <- ymd(substr(ccn_files,14,19))我如何在第一段代码中添加这一行,这样它才能做我想要的事情?
发布于 2020-02-06 09:11:23
我们可以使用Map
ccn_data_raw <- do.call(rbind, Map(cbind, lapply(ccn_files,
function(x) read.csv(paste("input/CCN/", x, sep=''),fill = TRUE,
header = TRUE, skip = 4)), date = test))或者使用purrr函数:
library(purrr)
ccn_data_raw <- map2_df(map(ccn_files, function(x)
read.csv(paste("input/CCN/", x, sep=''), fill = TRUE, header = TRUE,
skip = 4)), test, cbind)https://stackoverflow.com/questions/60086329
复制相似问题