[R] unlist( list( factor( 'a'), 1)) == c( 1, 1); unlist( list( factor( 'a'), factor( 1)))==c( 'a', '1')

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sun Mar 30 16:14:05 CEST 2014


This is definitely not an error, all these behaviours are by design. You need to (re)read the description of factors in the Introduction to R document that comes with the software. Factors are not "optimized versions of strings", they are finite sets of category information designed for use in statistical models. From this perspective most of the operations you are attempting are nonsensical  because each factor has a unique range of possibilities. If you don't know all of the possibilities when you read just one data frame then you should be using the stringsAsFactors=FALSE argument to the function you are creating the data frame with. Only when you have assembled all of your data into one data frame and are ready to reference categorical variables in a formula or plot should you convert  your strings to factors.
---------------------------------------------------------------------------
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.

On March 30, 2014 3:58:22 AM PDT, Frank Schwidom <schwidom at gmx.net> wrote:
>Hi,
>
>> c( factor( 'a'), ( 1))
>[1] 1 1
>> c( factor( 'a'), factor( 1))
>[1] 1 1
>> c( factor( 'a'), factor( 'b'))
>[1] 1 1
>
>> unlist( list( factor( 'a'), 1))
>[1] 1 1
>
>> unlist( list( factor( 'a'), factor( 1)))
>[1] a 1
>Levels: a 1
>
>> unlist( list( factor( 'a'), factor( 'b')))
>[1] a b
>Levels: a b
>
>
>In an data.frame it is the same
>
>> unlist( data.frame( factor( 'a'), factor( 1)))
>factor..a..   factor.1. 
>          a           1 
>Levels: a 1
>> unlist( data.frame( factor( 'a'), ( 1)))
>factor..a..        X.1. 
>          1           1 
>
>Im not sure, whether this behaviour can be 
>seen as an error. But if I for instance use
>
>read.table or scan then these functions
>will use  data.frame(...,
># with
> stringsAsFactors = default.stringsAsFactors())
>
>And so rows will by default appear, which
>contains factors and numbers.
>
>If i want to convert the content in an
>as.character datatype then the factor indexes
>will be used instead of the strings.
>
>This behaviour changes, if i switch off 
>the global Option 
>
>options( stringsAsFactors= FALSE)
>
>> unlist( data.frame( 'a', ( 1)))
>X.a. X.1. 
>
>> unlist( list( 'a', ( 1)))
>[1] "a" "1"
>
>
>This leads to some questions:
>
>If factors represent an
>optimized vector of strings (or other datatypes)
>why can an operation on this factor not behave
>like an operation on vector of strings (or ...)?
>
>And: what is the best solution to convert an 
>list( factor, number) to its character representation?
>
>Or: is it a bug?
>
>Regards
>
>______________________________________________
>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