R语言 从列表中读取csv文件,但添加文件名作为标识符

zpgglvta  于 2023-01-28  发布在  其他
关注(0)|答案(5)|浏览(187)

我有csv文件列表。我可以使用read_csv读取它们。
但是我想把文件名作为标识符添加进去,我该怎么做?

library(tidyverse)

# read file names
csv_filenames <- list.files(path = "OMITTED FOR THIS EXAMPLE", 
                            full.names = TRUE)

###
csv_filenames are "One.csv", "Two.csv", "Three.csv", ....
###

# read csv files
df <- read_csv(csv_filenames)
camsedfj

camsedfj1#

read_csv有一个自变量id =;如果指定“path”,则会得到一个名为“path”的列,其中包含以下文件名:

csv_data <- read_csv(csv_filenames, id = "path")

如果只需要基本文件名,可以添加dplyr::mutate步骤:

library(dplyr)
csv_data <- read_csv(csv_filenames, id = "path") %>%
  mutate(path = basename(path))
dphi5xsq

dphi5xsq2#

您应该能够在for循环中将assignbasename一起使用。

for(i in seq_along(csv_filenames)){
  assign(basename(csv_filenames)[i], read.csv(csv_filenames[i]))
}

使用basename将在全局环境中为新对象分配文件夹中文件的名称(而不是使用full.names = TRUE获得的整个文件路径)。

prdp8dxp

prdp8dxp3#

library(dplyr)

# list of file names
file_list <- list.files(path = "path/to/csv/files", pattern = "*.csv")

# read in all files and add the file name as an additional column
data_list <- lapply(file_list, function(x) {
  data <- read.csv(file = x, stringsAsFactors = FALSE) %>%
    mutate(file_name = x)
  return(data)
})
wydwbb8l

wydwbb8l4#

含R碱基

csv_files <- lapply(csv_filenames, read.csv)
file_names <- sub("\\..*", "", basename(csv_filenames))
out <- lapply(1:length(csv_files), function(i){
  transform(csv_files[[i]],  file_name = file_names[i])
})
do.call(rbind, out)
iyzzxitl

iyzzxitl5#

实际上,只是cbindbasename

lapply(csv_filenames, \(x) cbind(read.csv(x), file=basename(x))) |> do.call(what=rbind)
#   X1 X2 X3 X4      file
# 1  1  4  7 10 file1.csv
# 2  2  5  8 11 file1.csv
# 3  3  6  9 12 file1.csv
# 4  1  4  7 10 file2.csv
# 5  2  5  8 11 file2.csv
# 6  3  6  9 12 file2.csv
# 7  1  4  7 10 file3.csv
# 8  2  5  8 11 file3.csv
# 9  3  6  9 12 file3.csv
  • 数据:*
dat <- data.frame(matrix(1:12, 3, 4))
path <- '/what/so/ever'
lapply(1:3, \(i) write.csv(dat, paste0(path, '/tmp/file', i, '.csv'), row.names=F))
csv_filenames <- list.files(paste0(path, "/tmp"), full.names=TRUE)

相关问题