Importing multiple files into a single, merged data frame in R

爷,独闯天下 提交于 2021-02-07 09:38:53

问题


I have 8 CSV files all in the same directory, and need them importing into a single data frame in R. They all follow the same naming convention, "dataUK_1.csv", "dataUK_2.csv" etc., and have the exact same structure in terms of columns.

I've managed to create a vector of all the file names (including the full directory) by using:

files = list.files("/Users/iarwain/Data", pattern=".csv", full.names=T)

I'm just not sure how to pass these names to the read.csv command so that it loops 8 times, importing each file and adding its content as new rows into a single data frame, so that the end result is one data frame containing all rows of data from the 8 CSVs.

Thanks!


回答1:


You don't want a loop. You want lapply.

file_list <- list.files("/Users/iarwain/Data", pattern=".csv", full.names=T)


combined_files <- do.call("rbind", lapply(file_list, read.csv))

Translation: apply the function read.csv over each item in the list file_list. The output is a list. Call the function rbind on all of the output, and assign it to combined_files




回答2:


In tidyverse you can just add a pipe and a map_df()

file_list <- list.files("/Users/iarwain/Data", pattern=".csv", full.names=T) %>%
    map_df(read_csv(.))

Specifically, as Hadley describes here (about halfway down):

map_df(x, f) is effectively the same as do.call("rbind", lapply(x, f)) but under the hood is much more efficient.

and a thank you to Jake Kaupp for introducing me to map_df() here.



来源:https://stackoverflow.com/questions/28245156/importing-multiple-files-into-a-single-merged-data-frame-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!