Skip to content

How to create a new variable based on parts of another character variable.

4 messages · Philipp Fischer, jim holtman, Jim Lemon +1 more

#
On 10/24/2011 12:35 AM, Philipp Fischer wrote:
Hi Philipp,
Since you mentioned that you were just starting with R, it might be a 
little optimistic to throw you into the regular expression cage and 
expect you to emerge unscathed. You can do this by constructing a 2 
column matrix or data frame of replacement values:

replacements<-matrix(c("AWI","UFT","Arctic","Boreal"),ncol=2)
replacements
      [,1]  [,2]
[1,] "AWI" "Arctic"
[2,] "UFT" "Boreal"

Then write a function using grep to replace the values:

swapLabels<-function(x,y) {
  for(swaprow in 1:dim(y)[1])
   if(length(grep(y[swaprow,1],x))) return(y[swaprow,2])
  return(NA)
}

Finally, apply the function to the first row of the data frame:

pf.df$D<-unlist(lapply(pf.df[,1],swapLabels,replacements))
pf.df$D
[1] "Arctic" "Arctic" "Arctic" "Boreal" "Boreal" "Boreal"

Jim
#
Hi

If you want to get rid of regular expressions at all and your A values 
start AWI for Arctic and UFT for boreal you can

DF$D <- ifelse(substr(DF$A, 1,1) == "A", "Arctic", "Boreal")

Regards
Petr
problem
then
etc.
means
but
http://www.R-project.org/posting-guide.html