trying to prune down a list of files

Posted by romunov on Stack Overflow See other posts from Stack Overflow or by romunov
Published on 2010-05-13T13:17:14Z Indexed on 2010/05/13 13:24 UTC
Read the original article Hit count: 227

Filed under:
|

I have a list of files and I'm trying to extract all layer1_*.grd files. Is there a way of doing this in one grep expression?

lof <- c("layer1_1.grd", "layer1_1.gri", "layer1_2.grd", "layer1_2.gri", 
"layer1_3.grd", "layer1_3.gri", "layer1_4.grd", "layer1_4.gri", 
"layer1_5.grd", "layer1_5.gri", "layer2_1.grd", "layer2_1.gri", 
"layer2_2.grd", "layer2_2.gri", "layer2_3.grd", "layer2_3.gri", 
"layer2_4.grd", "layer2_4.gri", "layer2_5.grd", "layer2_5.gri", 
"layer3_1.grd", "layer3_1.gri", "layer3_2.grd", "layer3_2.gri", 
"layer3_3.grd", "layer3_3.gri", "layer3_4.grd", "layer3_4.gri", 
"layer3_5.grd", "layer3_5.gri", "layer4_1.grd", "layer4_1.gri", 
"layer4_2.grd", "layer4_2.gri", "layer4_3.grd", "layer4_3.gri", 
"layer4_4.grd", "layer4_4.gri", "layer4_5.grd", "layer4_5.gri"

I tried doing this in two steps:

list.of.files <- list.files(pattern = c("1_"))
list.of.files <- list.of.files[grep(".grd", list.of.files)]

Can someone enlighten me how to do this with grep in one step? I naively tried passing list() and c() to the grep but, as you can imagine, it doesn't work.

list.of.files <- list.files()
list.of.files <- list.of.files[grep(list("1_", ".grd"), list.of.files)]

© Stack Overflow or respective owner

Related posts about r

    Related posts about grep