r-读取并rbind多个csv文件,保留文档id



我正在尝试读取一个文件夹中具有相同结构的多个csv文件&我发现了这个问题,zx8754按照我的需要解决了它,但我意识到我必须在每个表上保留和id,才能得到类似于下面文件的东西

abc <- structure(c("1", "1", "1", "2", "2", "2", "3", "3", "3", "a", 
"a", "a", "b", "b", "b", "c", "c", "c", "a", "a", "a", "b", "b", 
"b", "c", "c", "c", "a", "a", "a", "b", "b", "b", "c", "c", "c"
), .Dim = c(9L, 4L))

我可以温和地获得一些指导,在下面的代码中为文件夹中的每个csv添加id列吗?

myMergedData <- 
do.call(rbind,
lapply(list.files(path = "N:/Ring data by cruise"), read.csv))

提前感谢!

您可以使用map_df创建id列。

myMergedData <- purrr::map_df(list.files(path = "N:/Ring data by cruise"), 
read.csv, .id = 'id')

如果你想在基本R:中这样做

data <- lapply(list.files(path = "N:/Ring data by cruise"), read.csv)
myMergedData <- Map(cbind, data, id = seq_along(data))

最新更新