Search code examples
rstring-matching

How do I mimic `ls foo*bar` in R?


Completely stumped, though it seems like a simple question that will be voted down very shortly as a duplicate, but I couldn't find the right "pattern" to search for the answer.

I am looking for files in a folder that match a dual pattern and then want to open them in R. So, assume list.files produces the following:

lf <- c("foo_23_bar.txt", "goo_42_mar.txt", "boo_42_bar.txt")

In command line, I would use ls foo*bar to find the first file, but in R, something like,

grep(paste("foo","bar",sep="|"),lf)

returns both files 1 and 3. I am not sure how to use the perl=TRUE option. Any help would be great. Thanks!


Solution

  • I usually use the system function. Check out here. http://stat.ethz.ch/R-manual/R-patched/library/base/html/system.html