Skip to content

Instantly share code, notes, and snippets.

@obrl-soil
Last active April 20, 2017 20:53
Show Gist options
  • Save obrl-soil/eb49ea0ca4df9634f2196072e49e1b21 to your computer and use it in GitHub Desktop.
Save obrl-soil/eb49ea0ca4df9634f2196072e49e1b21 to your computer and use it in GitHub Desktop.
library(tidyverse)
setwd('C:/DATA')
options(stringsAsFactors = FALSE)
# fake data
write.csv(tribble( ~col1, ~col2, ~col3,
1, 'a', T,
2, 'b', NA,
3, 'c', F),
file = file.path(getwd(), 'csv1.csv'),
row.names = FALSE)
write.csv(tribble( ~col1, ~col2, ~col3,
4, 'd', T,
5, 'e', NA,
6, 'f', F),
file = file.path(getwd(), 'csv2.csv'),
row.names = FALSE)
write.csv(tribble( ~col1, ~col2, ~col3,
7, 'g', T,
8, 'h', NA,
9, 'i', F),
file = file.path(getwd(), 'csv3.csv'),
row.names = FALSE)
# import
file_list <- list.files(getwd(), pattern = '^csv[123].csv$', full.names = TRUE)
names(file_list) <- tools::file_path_sans_ext(basename(file_list)) # shld be simpler...
# helpful: http://stackoverflow.com/questions/9950144/access-lapply-index-names-inside-fun
df <- do.call('rbind', sapply(seq_along(file_list), function(i) {
z <- read.csv(file_list[[i]], sep=',')
z$src <- names(file_list)[[i]]
return(z)
}, USE.NAMES = T, simplify = F))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment