Last active
November 12, 2015 14:05
-
-
Save ginolhac/6f11a2e6d60bb5cfafdf to your computer and use it in GitHub Desktop.
This file contains 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") | |
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