[Rd] c.factor

Matthew Dowle mdowle at concordiafunds.com
Tue Nov 14 17:36:59 CET 2006


Hi,

Given factors x and y,  c(x,y) does not seem to return a useful result :
> x
[1] a b c d e
Levels: a b c d e
> y
[1] d e f g h
Levels: d e f g h
> c(x,y)
 [1] 1 2 3 4 5 1 2 3 4 5
> 

Is there a case for a new method c.factor as follows?  Does something
similar exist already?  Is there a better way to write the function?

> c.factor = function(x,y)
{
    newlevels = union(levels(x),levels(y))
    m = match(levels(y), newlevels)
    ans = c(unclass(x),m[unclass(y)])
    levels(ans) = newlevels
    class(ans) = "factor"
    ans
}
> c(x,y)
 [1] a b c d e d e f g h
Levels: a b c d e f g h
> as.integer(c(x,y))
 [1] 1 2 3 4 5 4 5 6 7 8
>

Regards,
Matthew


> version
               _                           
platform       x86_64-unknown-linux-gnu    
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          2                           
minor          4.0                         
year           2006                        
month          10                          
day            03                          
svn rev        39566                       
language       R                           
version.string R version 2.4.0 (2006-10-03)



More information about the R-devel mailing list