Skip to content
Back to formatted view

Raw Message

Message-ID: <971536df050402041555226eb8@mail.gmail.com>
Date: 2005-04-02T12:15:01Z
From: Gabor Grothendieck
Subject: factor to numeric in data.frame
In-Reply-To: <3.0.6.32.20050402130157.00795180@pop.gmx.net>

Try this:

data.matrix(df.f12)

On Apr 2, 2005 6:01 AM, Heinz Tuechler <tuechler at gmx.at> wrote:
> Dear All,
> 
> Assume I have a data.frame that contains also factors and I would like to
> get another data.frame containing the factors as numeric vectors, to apply
> functions like sapply(..., median) on them.
> I read the warning concerning as.numeric or unclass, but in my case this
> makes sense, because the factor levels are properly ordered.
> I can do it, if I write for each single column "unclass(...), but I would
> like to use indexing, e.g. unclass(df[1:10]).
> Is that possible?
> 
> Thanks,
> Heinz T?chler
> 
> ## Example:
> f1 <- factor(c(rep('c1-low',2),rep('c2-med',5),rep('c3-high',3)))
> f2 <- factor(c(rep('c1-low',5),rep('c2-low',3),rep('c3-low',2)))
> df.f12 <- data.frame(f1,f2) # data.frame containing factors
> 
> ## this does work
> df.f12.num <- data.frame(unclass(df.f12[[1]]),unclass(df.f12[[2]]))
> df.f12.num
> ## this does not work
> df.f12.num <- data.frame(unclass(df.f12[[1:2]]))
> df.f12.num
> ## this does not work
> df.f12.num <- data.frame(unclass(df.f12[1:2]))
> df.f12.num
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>