问题
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 asdo.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