[R] Convert a factor to a numeric

arun smartpink111 at yahoo.com
Thu Oct 10 16:00:33 CEST 2013



Also, BTW, dat.num() is matrix, but if you use lapply(), it is still a dataframe.  Anyway, it depends on what the OP really wants as output.
dat.num <- apply(dat, 2, FUN = function(x) as.numeric(as.character(x)))
 dat[] <- lapply(dat,function(x) as.numeric(as.character(x)))

 str(dat)
'data.frame':    5 obs. of  4 variables:
 $ a     : num  1 2 3 4 5
 $ coef  : num  0.00566 0.00635 0.00369 0.00562 0.00637
 $ coef.l: num  0.00301 0.00334 0.00029 0.00209 0.0027
 $ coef.h: num  0.00831 0.00935 0.00708 0.00915 0.01004

 str(dat.num)
 num [1:5, 1:4] 1 2 3 4 5 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "a" "coef" "coef.l" "coef.h"
as.data.frame(dat.num)

A.K.



On Thursday, October 10, 2013 9:41 AM, Charles Determan Jr <deter088 at umn.edu> wrote:

Firstly, please make sure to reply-all so the r-help list also receives these emails.

Second, I have just run this sequence as it provides an exact copy with each as numeric.  Use the apply function, it iterates over each column and converts each to numeric.

dat <- read.table(text="a                coef               coef.l              coef.h
1   1   0.005657825001254  0.00300612956318132 0.00830952043932667
2   2 0.00634505314577229  0.00334102345418614 0.00934908283735844
3   3 0.00368668099805019 0.000289702228748421 0.00708365976735195
4   4  0.0056200291035751  0.00209123538827368 0.00914882281887651
5   5 0.00636609791030242  0.00269683889899591  0.0100353569216089",sep="",colClasses=rep("factor",4))

dat.num <- apply(dat, 2, FUN = function(x) as.numeric(as.character(x)))

Charles





On Thu, Oct 10, 2013 at 8:37 AM, arun <smartpink111 at yahoo.com> wrote:


>
>Looks like it is directly doing:
>as.numeric() without the as.character()
>For ex:
> as.numeric(dat[,2])
>#[1] 3 4 1 2 5
>
>
>
>
>
>
>On Thursday, October 10, 2013 9:33 AM, Charles Determan Jr <deter088 at umn.edu> wrote:
>
>I'm not honestly sure why data.matrix didn't work off hand.  Perhaps another user can shed some light on this.  An alternative is the following:
>
>apply(dat, 2, FUN = function(x) as.numeric(as.character(x)))
>
>
>
>
>On Thu, Oct 10, 2013 at 8:26 AM, arun <smartpink111 at yahoo.com> wrote:
>
>Did you mean to apply it like this or is it something else?
>> data.matrix(dat) #
>>  a coef coef.l coef.h
>>1 1    3      4      2
>>2 2    4      5      4
>>3 3    1      1      1
>>4 4    2      2      3
>>5 5    5      3      5
>>
>>
>>A.K.
>>
>>
>>
>>
>>
>>
>>On Thursday, October 10, 2013 9:09 AM, Charles Determan Jr <deter088 at umn.edu> wrote:
>>
>>data.matrix() should do the job for you
>>
>>Charles
>>
>>
>>
>>
>>On Thu, Oct 10, 2013 at 8:02 AM, arun <smartpink111 at yahoo.com> wrote:
>>
>>Hi,
>>>It is not clear whether all the variables are factor or only a few are..
>>>
>>>dat<- read.table(text="a                coef               coef.l              coef.h
>>>1   1   0.005657825001254  0.00300612956318132 0.00830952043932667
>>>2   2 0.00634505314577229  0.00334102345418614 0.00934908283735844
>>>3   3 0.00368668099805019 0.000289702228748421 0.00708365976735195
>>>4   4  0.0056200291035751  0.00209123538827368 0.00914882281887651
>>>5   5 0.00636609791030242  0.00269683889899591  0.0100353569216089",sep="",colClasses=rep("factor",4))
>>>dat1<- dat
>>>
>>>
>>> dat[] <- lapply(dat,function(x) as.numeric(as.character(x)))
>>>
>>>str(dat)
>>>#'data.frame':    5 obs. of  4 variables:
>>># $ a     : num  1 2 3 4 5
>>># $ coef  : num  0.00566 0.00635 0.00369 0.00562 0.00637
>>># $ coef.l: num  0.00301 0.00334 0.00029 0.00209 0.0027
>>># $ coef.h: num  0.00831 0.00935 0.00708 0.00915 0.01004
>>>
>>>
>>># With only a subset of variables in the dataset as factors
>>> dat1$a<- as.numeric(as.character(dat1$a))
>>>
>>> 
>>>dat1[sapply(dat1,is.factor)]<- lapply(dat1[sapply(dat1,is.factor)],function(x) as.numeric(as.character(x)))
>>> str(dat1)
>>>#'data.frame':    5 obs. of  4 variables:
>>># $ a     : num  1 2 3 4 5
>>># $ coef  : num  0.00566 0.00635 0.00369 0.00562 0.00637
>>># $ coef.l: num  0.00301 0.00334 0.00029 0.00209 0.0027
>>># $ coef.h: num  0.00831 0.00935 0.00708 0.00915 0.01004
>>> 
>>>A.K.
>>>
>>>
>>>
>>>I have a factor data frame which I want to convert to numeric without any change in contents. How could I do that?
>>>
>>>
>>>   a                coef               coef.l              coef.h
>>>1   1   0.005657825001254  0.00300612956318132 0.00830952043932667
>>>2   2 0.00634505314577229  0.00334102345418614 0.00934908283735844
>>>3   3 0.00368668099805019 0.000289702228748421 0.00708365976735195
>>>4   4  0.0056200291035751  0.00209123538827368 0.00914882281887651
>>>5   5 0.00636609791030242  0.00269683889899591  0.0100353569216089
>>>
>>>______________________________________________
>>>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.
>>>
>>
>
>
>--
>
>Charles Determan
>Integrated Biosciences PhD Candidate
>University of Minnesota
>


-- 

Charles Determan
Integrated Biosciences PhD Candidate
University of Minnesota



More information about the R-help mailing list