有两个快速的方法来读取多个文件,并把它们放在一个单独的数据帧或data.table
使用fread
从data.table
包
# List all txt files including sub-folders
list_of_files <- list.files(path = ".", recursive = TRUE,
pattern = "\\.txt$", full.names = TRUE)
library(data.table)
# Read all the files and create a FileName column to store filenames
DT <- rbindlist(sapply(list_of_files, fread, simplify = FALSE),
use.names = TRUE, idcol = "FileName")
使用purrr::map_df
和readr::read_table2
从tidyverse
包:
library(tidyverse)
# Read all the files and create a FileName column to store filenames
df <- list_of_files %>%
set_names(.) %>%
map_df(read_table2, .id = "FileName")
注意:要清理文件名,请使用basename
或gsub
函数
问题是'txt'不是函数。您指向的链接是关于'read.csv'函数的。感谢 – Wok 2010-08-03 17:56:58