R导入多个csv文件

R import multiple csv files

我想在 R 中导入多个 TSV 文件(是的:TSV)

使用以下方法读取包含特定列的单个文件效果很好:

data00<-read.csv(file = '/Volumes/2018/06_abteilungen/bi/analytics/tools/adobe/adobe_analytics/adobe_analytics_api_rohdaten/api_via_data_feed_auf_ftp/beispiel_datenexporte_data_feed/01sssamsung4de_20180501-000000.tsv',
                 sep ="\t",
             fill = TRUE,
             quote='', 
             header = FALSE 
)[ ,c(287, 288, 289, 290, 291, 292, 293, 304, 370, 661, 662, 812, 813,     994, 995, 1002)]

现在我想导入多个文件并将它们合并到一个数据帧中:

setwd('/Volumes/2018/06_abteilungen/bi/analytics/tools/adobe/adobe_analytics/adobe_analytics_api_rohdaten/api_via_data_feed_auf_ftp/beispiel_datenexporte_data_feed/import_r')

temp <- list.files(pattern="*.tsv")

test_data <- lapply(temp, read.csv,
sep ="\t",
fill = TRUE,
quote='', 
header = FALSE 
)[ ,c(287, 288, 289, 290, 291, 292, 293, 304, 370, 661, 662, 812, 813, 994, 995, 1002)]

上次查询给了我一个例外并且不起作用: Fehler in lapply(temp, read.csv, sep = "\t", fill = TRUE, quote = "", header = FALSE)[ : falsche Anzahl von Dimensionen(翻译:维数错误)

当我获取所有列时,它起作用了:

test_data <- lapply(temp, read.csv,
sep ="\t",
fill = TRUE,
quote='', 
header = FALSE 
)

您索引的是数据框列表,而不是数据框本身。尝试:

test_data <- lapply(temp,function(x){
  read.csv(file = x,
                 sep ="\t",
                 fill = TRUE,
                 quote='', 
                 header = FALSE 
  )[ ,c(287, 288, 289, 290, 291, 292, 293, 304, 370, 661, 662, 812, 813,994, 995, 1002)]
}
)

很难说没有样本数据,但我相信您必须先 'merge' 导入列表:

dplyr 解决方案:

library(dplyr)
test_data <- lapply(temp, read.csv,
                    sep ="\t",
                    fill = TRUE,
                    quote='', 
                    header = FALSE 
                    ) %>%  
  bind_rows() %>%
  select( c(287, 288, 289, 290, 291, 292, 293, 304, 370, 661, 662, 812, 813, 994, 995, 1002) )