[R] recode according to specific sequence of characters within a string variable

Greg Snow Greg.Snow at imail.org
Fri Feb 4 18:52:41 CET 2011


You can do this with regular expressions, since you want to extract specific values from the string I would suggest learning about the gsubfn package, it is a bit easier with gsubfn than with the other matching tools. 


-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of D. Alain
> Sent: Friday, February 04, 2011 5:33 AM
> To: r-help at r-project.org
> Subject: [R] recode according to specific sequence of characters within
> a string variable
> 
> Dear R-List,
> 
> I have a dataframe with one column "name.of.report" containing
> character values, e.g.
> 
> 
> >df$name.of.report
> 
> "jeff_2001_teamx"
> "teamy_jeff_2002"
> "robert_2002_teamz"
> "mary_2002_teamz"
> "2003_mary_teamy"
> ...
> (i.e. the bit of interest is not always at same position)
> 
> Now I want to recode the column "name.of.report" into the variables
> "person", "year","team", like this
> 
> >new.df
> 
> "person"  "year"  "team"
> jeff           2001      x
> jeff           2002      y
> robert       2002      z
> mary        2002      z
> 
> I tried with grep()
> 
> df$person<-grep("jeff",df$name.of.report)
> 
> but of course it didn't exactly result in what I wanted to do. Could
> not find any solution via RSeek. Excuse me if it is a very silly
> question, but can anyone help me find a way out of this?
> 
> Thanks a lot
> 
> Alain
> 
> 
> 
> 
> 	[[alternative HTML version deleted]]



More information about the R-help mailing list