Loading data with a selection on date in filename in R

Viewed 33

I have the following code that loads in several .txt files from a folder

namesnotxt = substr(filenames,1,24)

for(i in namesnotxt){
  filepath = file.path(" ", paste(i, ".txt", sep=""))
  assign(i, read.table(filepath,
                       skip= 20, #skips the first 20 rows of data because of the txt. file structure
                       col.names=( c("time","v","a","t1","t2","t3","t4","t5","t6","t7","t8")), #names the data columns
                       sep=","))}

I want this code to be selective on the date of the files, which is given in the filenames as follows: SD07_TWK_20190822_003004

I know I can get the dates in these filenames with: date = substr(filenames,10,17)

However I don't know how to build this into the loop as a selection. I have tried with an IF statement in the jist of:

date_start = "20190822"
date_end = "20190823"

if( date >= date_start && date <= date_end){
  print(date)}

However, I can't get that to work.

The date-time can also be found in the file itself (on the 14th row), which looks as follows: Maybe this would offer an alternative way of doing this.

Datetime=2019-08-22 00:30:04

1 Answers

The below will work, using lubridate.

library(lubridate)
#> Warning: package 'lubridate' was built under R version 4.0.5
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
date_start = "20190822"
date_end = "20190823"
myname <- 'SD07_TWK_20190822_003004'

date_start <- lubridate::as_date(date_start)
date_end <- lubridate::as_date(date_end)

mydate <- ymd(strsplit(myname,'_')[[1]][3]) #use ymd for more specificity

mydate >= date_start & mydate <= date_end
#> [1] TRUE

Created on 2022-08-11 by the reprex package (v2.0.1)


EDIT you can use something like the below to load data

mynames <- list.files(pattern='.txt', full.names = T)
mydates <- sapply(mynames, function(x) ymd(strsplit(x,'_')[[1]][3]))
Related