[R] rbind.data.frame drops attributes for factor variables

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Fri Nov 11 18:44:38 CET 2011


As the doctor says, if it hurts "don't do that".

A factor is a sequence of integers with a corresponding list of character strings. Factors in two separate vectors can and usually do map the same integer to different strings, and R cannot tell how you want that resolved.

Convert these columns to character before combining them, and only convert to factor when you have all of your possibilities present (or you specify them in the creation of the factor vector).
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Sammy Zee <szee2007 at gmail.com> wrote:

>Hi all,
>
>When I use rbind() or rbind.data.frame() to add a row to an existing
>dataframe, it appears that attributes for the column of type "factor"
>are
>dropped. I see the following post with same problem. However i did not
>see
>any reply to the following posting offering a solution. Could someone
>please help.
>
>http://r.789695.n4.nabble.com/rbind-data-frame-drops-attributes-for-factor-variables-td919575.html
>
>Thanks,
>Sanjay
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list