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('c1low',2),rep('c2med',5),rep('c3high',3)))
f2 < factor(c(rep('c1low',5),rep('c2low',3),rep('c3low',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] factor to numeric in data.frame
Tweet 

Search Discussions

Gabor Grothendieck at Apr 2, 2005 at 12:15 pm Try this:
data.matrix(df.f12)On Apr 2, 2005 6:01 AM, Heinz Tuechler 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('c1low',2),rep('c2med',5),rep('c3high',3)))
f2 < factor(c(rep('c1low',5),rep('c2low',3),rep('c3low',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
______________________________________________
Rhelp at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/rhelp
PLEASE do read the posting guide! http://www.Rproject.org/postingguide.html 
Heinz Tuechler at Apr 2, 2005 at 12:31 pm Perfect! This is exactly what I needed.At 07:15 02.04.2005 0500, Gabor Grothendieck wrote:
Try this:
data.matrix(df.f12)
Many thanks,
Heinz T?chlerhttp://www.Rproject.org/postingguide.htmlOn Apr 2, 2005 6:01 AM, Heinz Tuechler 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('c1low',2),rep('c2med',5),rep('c3high',3)))
f2 < factor(c(rep('c1low',5),rep('c2low',3),rep('c3low',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
______________________________________________
Rhelp at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/rhelp
PLEASE do read the posting guide!

Prof Brian Ripley at Apr 2, 2005 at 1:26 pm Yes: unclass is applied to a column and not the data frame.On Sat, 2 Apr 2005, Heinz Tuechler 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?
newdf < df
newdf[1:10] < lapply(newdf[1:10], unclass)
BTW, please read the posting guide, and do not say `does not work' when it
patently does work as documented.Thanks,That also works: unclassing a data frame gives a list.
Heinz T?chler
## Example:
f1 < factor(c(rep('c1low',2),rep('c2med',5),rep('c3high',3)))
f2 < factor(c(rep('c1low',5),rep('c2low',3),rep('c3low',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]]))
Yes, it does work. What do you think [[1:2]] does? Please RTFM.
## this does not work
df.f12.num < data.frame(unclass(df.f12[1:2]))
df.f12.num

Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595 
Heinz Tuechler at Apr 2, 2005 at 12:43 pm Thank you for your answer. I am sorry for the unprecise formulation `doesAt 14:26 02.04.2005 +0100, Prof Brian Ripley wrote:On Sat, 2 Apr 2005, Heinz Tuechler wrote:Yes: unclass is applied to a column and not the data frame.
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?
newdf < df
newdf[1:10] < lapply(newdf[1:10], unclass)
BTW, please read the posting guide, and do not say `does not work' when it
patently does work as documented.
not work'. I intended `does not solve my problem'.
In the meantime Gabor Grothendieck responded with:
'Try this: data.matrix(df.f12)'
which is exactly, what I was searching for.
Many thanks,
Heinz T?chlerThanks,That also works: unclassing a data frame gives a list.
Heinz T?chler
## Example:
f1 < factor(c(rep('c1low',2),rep('c2med',5),rep('c3high',3)))
f2 < factor(c(rep('c1low',5),rep('c2low',3),rep('c3low',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]]))
Yes, it does work. What do you think [[1:2]] does? Please RTFM.
## this does not work
df.f12.num < data.frame(unclass(df.f12[1:2]))
df.f12.num

Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
Related Discussions
Discussion Navigation
view  thread  post 
Discussion Overview
group  rhelp 
categories  r 
posted  Apr 2, '05 at 11:01a 
active  Apr 2, '05 at 1:26p 
posts  5 
users  3 
website  rproject.org 
irc  #r 