regex - Read a list of text files in R based on part of their name -


i using below lines read 1 text file. but, need include read list of text files not all. how can achieve in r?

i have multiple folders in c:/data path. ie. 1998_data, 1999_data....so on , each folder has text files 01.txt...49.txt.

thanks in advance.

startingdir<-"c:/data" files <- list.files(path = startingdir,pattern = "24.txt",recursive=t,full.names=t) 

i included line below not read 3 files in list. suggestions?

my_files <- c("24.txt","01.txt","10.txt") files <- list.files(path = startingdir,pattern = my_files,recursive=t,full.names=t) 

that's because pattern expects regular expression:

thepattern <- "24\\.txt|01\\.txt|10\\.txt" files <- list.files(path = startingdir,                     pattern = thepattern,                     recursive=true,                     full.names=true) 

keep in mind have escape dot, otherwise interpreted "anything".

if want automate this, can following:

my_files <- c("24.txt","01.txt","10.txt") my_files <- gsub(".","\\.",my_files, fixed = true) my_pattern <- paste(my_files, collapse = "|") 

you need fixed = true in gsub avoid dot read "anything". see ?regex


Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -

c# - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind -