[R] Maintaining factors when copying from one data frame to another

Tobias Verbeke tobias.verbeke at telenet.be
Wed Dec 7 19:04:15 CET 2005


Kurt Wollenberg wrote:

>Greetings all:
>
>OK, this is bugging the @#@%* out of me. I know the answer is simple
>and straightforward but for the life of me I cannot find it in the
>documentation, in the archives, or in my notes (because I know I've
>encountered this in the past). My problem is:
>
>I have a data frame with columns A, B, C, D, and E. A, B, and E are
>factors and C and D are numeric. I need a new data frame with just A,
>C, and D. When I copy these columns to a new data frame
>
>  
>
>>newDF <- data.frame(cbind(oldDF$A, oldDF$C, oldDF$D))
>>    
>>
>
>all the factor data comes out as levels rather than the original
>factors. How do I preserve the factors when I copy from one data frame
>to another?
>
>  
>
The following may be better:

newDF <- subset(oldDF, select = c(A, C, D))

HTH,
Tobias




More information about the R-help mailing list