Skip to content

Instantly share code, notes, and snippets.

@ginolhac
Last active November 12, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ginolhac/6f11a2e6d60bb5cfafdf to your computer and use it in GitHub Desktop.
Save ginolhac/6f11a2e6d60bb5cfafdf to your computer and use it in GitHub Desktop.
library("dplyr")
library("readr")
library("stringi")
# load file with names absolute paths
file.list <- list.files("/Volumes/gaia/", pattern='*.txt', full.names = TRUE)
# read them all as a list
df.list <- lapply(file.list, read_tsv)
# name list with correct id extracted from filenames
names(df.list) <- stri_extract_first(basename(file.list), regex = "patient_\\d")
# rbind as single data.frame, id column is filled with extracted info in filenames
df <- bind_rows(df.list, .id = "id")
# as function
load_data <- function(abspath, ext, pattern){
file.list <- list.files(abspath,
pattern = ext, full.names = TRUE) # load file names absolute paths
df.list <- lapply(file.list, read_tsv) # read them all as a list
names(df.list) <- stri_extract_first(basename(file.list), regex = pattern) # name list with correct id
bind_rows(df.list, .id = "id")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment