有兩個快速的方法來讀取多個文件,並把它們放在一個單獨的數據幀或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