[R] gsub syntax help
I have some elements in a vector with extraneous information (e.g. file name and sample IDs) that I'd like to strip from every element. For example, I would like SPI1.S1.str1.P3.sample.tif SPI1.S1.STR2.P1.sample.tif to read SPI1.S1.str1.P3 SPI1.S1.STR2.P1. Will someone help me with the syntax in gsub? It needs to be something like gsub(garbage, everything except garbage , dataframe,), I think, but it's the everything except garbage that's giving me trouble. Thanks *Ben Caldwell* [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] gsub syntax help
In the example you gave, all that has to be done is replace .sample.tif at the end of the string with , which is easy. avec - c(SPI1.S1.str1.P3.sample.tif, SPI1.S1.STR2.P1.sample.tif) gsub(\\.sample\\.tif$, , avec) [1] SPI1.S1.str1.P3 SPI1.S1.STR2.P1 If your real data are more complex, we need to know what they look like. Sarah On Thu, Feb 2, 2012 at 4:42 PM, Benjamin Caldwell btcaldw...@berkeley.edu wrote: I have some elements in a vector with extraneous information (e.g. file name and sample IDs) that I'd like to strip from every element. For example, I would like SPI1.S1.str1.P3.sample.tif SPI1.S1.STR2.P1.sample.tif to read SPI1.S1.str1.P3 SPI1.S1.STR2.P1. Will someone help me with the syntax in gsub? It needs to be something like gsub(garbage, everything except garbage , dataframe,), I think, but it's the everything except garbage that's giving me trouble. Thanks *Ben Caldwell* -- Sarah Goslee http://www.functionaldiversity.org __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] gsub syntax help
Oh, perfect. I was running gsub(.sample.tif, , avec). your change gsub(\\.sample\\.tif$, , avec) did it. Thanks Sarah *Ben Caldwell* On Thu, Feb 2, 2012 at 1:48 PM, Sarah Goslee sarah.gos...@gmail.com wrote: In the example you gave, all that has to be done is replace .sample.tif at the end of the string with , which is easy. avec - c(SPI1.S1.str1.P3.sample.tif, SPI1.S1.STR2.P1.sample.tif) gsub(\\.sample\\.tif$, , avec) [1] SPI1.S1.str1.P3 SPI1.S1.STR2.P1 If your real data are more complex, we need to know what they look like. Sarah On Thu, Feb 2, 2012 at 4:42 PM, Benjamin Caldwell btcaldw...@berkeley.edu wrote: I have some elements in a vector with extraneous information (e.g. file name and sample IDs) that I'd like to strip from every element. For example, I would like SPI1.S1.str1.P3.sample.tif SPI1.S1.STR2.P1.sample.tif to read SPI1.S1.str1.P3 SPI1.S1.STR2.P1. Will someone help me with the syntax in gsub? It needs to be something like gsub(garbage, everything except garbage , dataframe,), I think, but it's the everything except garbage that's giving me trouble. Thanks *Ben Caldwell* -- Sarah Goslee http://www.functionaldiversity.org [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.