Skip to content

Instantly share code, notes, and snippets.

@kissmygritts
Last active March 24, 2020 03:03
Show Gist options
  • Save kissmygritts/685b9704dd8f36988a08569375f349d1 to your computer and use it in GitHub Desktop.
Save kissmygritts/685b9704dd8f36988a08569375f349d1 to your computer and use it in GitHub Desktop.
telemetry_dir <- here::here('data', 'telemetry')
# simplest?
files <- dir(telemetry_dir)
grep_files <- files[!(is.na(stringr::str_extract(files, 'GSM\\d*.csv')))]
df <- tibble::tibble()
for(i in seq_along(grep_files)) {
d <- readr::read_csv(file.path(telemetry_dir, grep_files[i]))
df <- rbind(df, d)
rm(d)
}
# how I wrote it
files <- Filter(x = dir(telemetry_dir),
f = function (x) !(is.na(stringr::str_extract(x, 'GSM\\d*.csv')))
)
dat <- Reduce('rbind', lapply(files, function (x) {
readr::read_csv(file.path(telemetry_dir, x))
}))
# one liner
dat2 <- Reduce(
f = 'rbind',
x = lapply(
X = dir(telemetry_dir)[!(is.na(stringr::str_extract(dir(telemetry_dir), 'GSM\\d*.csv')))],
FUN = function (x) {
readr::read_csv(file.path(telemetry_dir, x))
})
)
# as a function, and for clarity
read_telemetry <- function (directory, pattern) {
## get all files in a directory
files <- dir(directory)
## filter files to return those matching the pattern
files <- files[!(is.na(stringr::str_extract(files, pattern)))]
## loop over files, read from directory with read_csv and
## reduce into a data frame with the rbind function
dat <- Reduce('rbind', lapply(files, function (x) {
## change this to any IO function, maybe sf::read_sf, read.table, etc...
readr::read_csv(file.path(directory, x))
}))
}
dat3 <- read_telemetry(directory = telemetry_dir, pattern = 'GSM\\d*.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment