Alexander.Herr at csiro.au
2007-Jul-06 02:40 UTC
[R] ?replace characters within vector data
Hi List, I want replace characters within a vector. Outside R I could use sed, but I'd like to automate it in R. For example vectorx xxxyyz xxxyyza xxxyyzzb I want to change to: vectorx aaayyz aaayyza aaayyzzb The obvious replace command only deals with whole data entries? Any hints would be appreciated. Thanks Herry
On Fri, 2007-07-06 at 12:40 +1000, Alexander.Herr at csiro.au wrote:> Hi List, > > I want replace characters within a vector. Outside R I could use sed, > but I'd like to automate it in R. For example > > vectorx > xxxyyz > xxxyyza > xxxyyzzb > > I want to change to: > > vectorx > aaayyz > aaayyza > aaayyzzb > > The obvious replace command only deals with whole data entries? > Any hints would be appreciated. > > Thanks > HerrySee ?gsub and also ?regex> vectorx[1] "xxxyyz" "xxxyyza" "xxxyyzzb"> gsub("x", "a", vectorx)[1] "aaayyz" "aaayyza" "aaayyzzb" HTH, Marc Schwartz
Check out ?chartr On 7/5/07, Alexander.Herr at csiro.au <Alexander.Herr at csiro.au> wrote:> Hi List, > > I want replace characters within a vector. Outside R I could use sed, > but I'd like to automate it in R. For example > > vectorx > xxxyyz > xxxyyza > xxxyyzzb > > I want to change to: > > vectorx > aaayyz > aaayyza > aaayyzzb > > The obvious replace command only deals with whole data entries? > Any hints would be appreciated. > > Thanks > Herry > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
sub("xxx", "aaa", vectorx) or maybe gsub, depending on your application. Cheers, Simon. On Fri, 2007-07-06 at 12:40 +1000, Alexander.Herr at csiro.au wrote:> Hi List, > > I want replace characters within a vector. Outside R I could use sed, > but I'd like to automate it in R. For example > > vectorx > xxxyyz > xxxyyza > xxxyyzzb > > I want to change to: > > vectorx > aaayyz > aaayyza > aaayyzzb > > The obvious replace command only deals with whole data entries? > Any hints would be appreciated. > > Thanks > Herry > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Simon Blomberg, BSc (Hons), PhD, MAppStat. Lecturer and Consultant Statistician Faculty of Biological and Chemical Sciences The University of Queensland St. Lucia Queensland 4072 Australia Room 320, Goddard Building (8) T: +61 7 3365 2506 email: S.Blomberg1_at_uq.edu.au The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. - John Tukey.