Hugo Bendini Hugo Bendini -4 years ago 143
R Question

How to substring a char vector using patterns in R?

I have this kind of char vector: "MODIS.evi.2013116.yL2.BOKU.tif". The number in the middle of the vector is gonna change. And the "evi" word will change to "ndvi" some times. I want to use substr (or other function, maybe) to substring the vector after the second point: ".", ie, just take the "2013116.yL2.BOKU.tif", even when the string is "MODIS.evi.2013116.yL2.BOKU.tif" or "MODIS.ndvi.2013116.yL2.BOKU.tif".

Answer Source

We can use sub to match two instance of one or more characters that are not a . followed by a . from the start (^) of the string and replace it with blank ("")

sub("^([^.]+\\.){2}", "", str1)
#[1] "2013116.yL2.BOKU.tif" "2013116.yL2.BOKU.tif"

If the pattern to keep always start with numbers, then the above can be simplified to match only one or more non-numeric characters and replace it with blank from the start (^) of the string

sub("^\\D+", "", str1)
#[1] "2013116.yL2.BOKU.tif" "2013116.yL2.BOKU.tif"


str1 <- c("MODIS.evi.2013116.yL2.BOKU.tif", "MODIS.ndvi.2013116.yL2.BOKU.tif")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download