Last active
March 27, 2017 12:10
-
-
Save Gijs-Koot/075bb5b7865fbd98be4defe67ad5bcc8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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