Skip to content
Prev 274729 / 398506 Next

Function to "lump" factors together?

On Oct 18, 2011, at 05:36 , David Winsemius wrote:

            
pmatch() should work for alternatives of the FOO* style, yes. For a full, vectorized, version of the "case", I'd expect that one ticket is a nesting of 

x2 <- ifelse ( grepl(p1,x), s1 ,
   ifelse ( grepl(p2,x), s2 ,
...
         ifelse ( grepl(pn,x),sn ,"UNK")
    )
  ) 

(The patterns p1,..., pn are regexps, but glob2rx() exists). Also notice that it may be easier to work on the level set of the original factor rather than converting the whole thing to character and back:

x <- levels(f)
x2 <- ifelse(....)
f2 <- f
levels(f2) <- x2 

It should also be possible to think up a version that avoids the nesting of ifelse. Something like

m <- sapply(patterns, grepl, x) 
first <- .... 
x2 <- replacements[first]

the "...." is the tricky bit: Get the index of the first TRUE element in each column of a logical matrix. It can be done straightforwardly with apply() and match(), but any more efficient variants escape me just now.