Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Gijs-Koot/075bb5b7865fbd98be4defe67ad5bcc8 to your computer and use it in GitHub Desktop.
Save Gijs-Koot/075bb5b7865fbd98be4defe67ad5bcc8 to your computer and use it in GitHub Desktop.
library(dplyr)
## use a vector for the files and the dataframes, where we leave a column in some of them
fn <- c("test_1.csv", "test_2.csv", "test_3.csv")
dl <- c(
mtcars %>% select(-gear),
mtcars %>% select(-disp),
mtcars %>% select(-wt)
)
# write all the files
for(i in 1:3){
dl[[i]] %>% write.csv(fn[[i]])
}
# use Map to read all files in one line
sets <- Map(read.csv, fn)
# or equivalently
sets <- list(
read.csv(fn[[1]]),
read.csv(fn[[2]]),
read.csv(fn[[3]])
)
# then use Reduce to combine the sets
# they have different columns,
# but noone is complaining, only someone is silently inserting NA's
full.set <- Reduce(bind_rows, sets)
# or equivalently
full.set <- sets[[1]] %>% bind_rows(sets[[2]]) %>% bind_rows(sets[[3]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment