[Rd] c.factor

From: Matthew Dowle <mdowle_at_concordiafunds.com>
Date: Tue 14 Nov 2006 - 16:36:59 GMT

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)

R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed Nov 15 04:05:37 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 14 Nov 2006 - 18:30:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.