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

Frank Schwidom schwidom at gmx.net
Sun Mar 30 12:58:22 CEST 2014


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



More information about the R-help mailing list