[R] Stack with factors

Ken Knoblauch ken.knoblauch at inserm.fr
Thu Apr 1 11:53:50 CEST 2010


Kenneth Roy Cabrera Torres <krcabrer <at> une.net.co> writes:
> Hi R users:
> I found that I cannot stack() a data.frame with factors.
> db1<-data.frame(replicate(6,factor(sample(c("A","B"),6,replace=TRUE))))
> str(db1)
> db2<-stack(db1)
> db2
> "db2" does not have any row.
> How can I stack them by the variables X1,X2,...,X6?

you can see what is happening in stack.data.frame 
you have a line

x <- x[, unlist(lapply(x, is.vector)), drop = FALSE]

and

lapply(x, is.vector)) 

is applied to each column of the data frame but
you can verify for yourself that a factor yields FALSE here

x <- db1[[1]]
is.vector(x)
[1] FALSE

so I think that this at least explains why it doesn't work as 
you expected.

> Thank you for your help.
> 
> Kenneth


-- 
Ken Knoblauch
Inserm U846
Stem-cell and Brain Research Institute
Department of Integrative Neurosciences
18 avenue du Doyen Lépine
69500 Bron
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: +33 (0)6 84 10 64 10
http://www.sbri.fr/members/kenneth-knoblauch.html



More information about the R-help mailing list