Re: [R] conversion factor into numeric

From: Douglas Bates <bates_at_stat.wisc.edu>
Date: Fri 06 May 2005 - 22:48:32 EST

Dimitris Rizopoulos wrote:
> I presume that in the place of "1,020" you need "1.02".

In this context I believe that "1,020" means "1020" in which case the gsub call could be as.numeric(gsub(",", "", levels(mass)))[mass]

> Then use
> something like this:
> 
> mass <- factor(c("800", "1,020", "1,130", "1,250"))
> as.numeric(gsub(",", ".", as.character(mass)))
> 
> I hope it helps.
> 
> Best,
> Dimitris
> 
> ----
> Dimitris Rizopoulos
> Ph.D. Student
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
> 
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/16/336899
> Fax: +32/16/337015
> Web: http://www.med.kuleuven.ac.be/biostat/
>     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
> 
> 
> ----- Original Message ----- From: "Smit, R. (Robin) (IenT)"
> <robin.smit@tno.nl>
> To: <r-help@stat.math.ethz.ch>
> Sent: Friday, May 06, 2005 2:17 PM
> Subject: [R] conversion factor into numeric
> 
> 

>> Thank you all for your (fast) comments.
>>
>> Unfortunately I could not make the advise work:
>>
>>> mass
>>
>> [1] 800 800 800 800 800 800 800 800 800 800 800
>> 800 800 800 800 800 800 800 910 910 910 910 910
>> 910 910
>> [26] 910 910 910 910 910 910 910 910 910 910 910
>> 910 910 910 1,020 1,020 1,020 1,020 1,020 1,020 1,020 1,020 1,020
>> 1,020 1,020
>> [51] 1,020 1,020 1,020 1,020 1,020 1,020 1,020 1,130 1,130 1,130 1,130
>> 1,130 1,130 1,130 1,130 1,130 1,130 1,130 1,130 1,130 1,130 1,130 1,130
>> 1,130 1,130
>> [76] 1,250 1,250 1,250 1,250 1,250 1,250 1,250 1,250 1,250 1,250 1,250
>> 1,250 1,250 1,250 1,250 1,250 1,250 1,250 1,250 1,250 1,250 1,250 1,250
>> 1,250 1,250
>> [101] 1,250 1,250 1,250 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360
>> 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360
>> 1,360 1,360
>> [126] 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360
>> 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360 1,360
>> 1,360 1,360
>> [151] 1,360 1,360 1,360 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470
>> 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470
>> 1,470 1,470
>> [176] 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470
>> 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470 1,470
>> 1,470 1,470
>> [201] 1,470 1,470 1,470 1,590 1,590 1,590 1,590 1,590 1,590 1,590 1,590
>> 1,590 1,590 1,590 1,590 1,590 1,590 1,590 1,590 1,590 1,590 1,590 1,590
>> 1,590 1,810
>> [226] 1,810 1,810 1,810 1,810 1,810 1,810 1,810 1,810 1,810 1,810 1,810
>> 1,810
>> Levels: 1,020 1,130 1,250 1,360 1,470 1,590 1,810 800 910
>>
>>> str(mass)
>>
>> Factor w/ 9 levels "1,020","1,130",..: 8 8 8 8 8 8 8 8 8 8 ...
>>
>>> as.numeric(as.character(mass))
>>
>> [1] 800 800 800 800 800 800 800 800 800 800 800 800 800 800 800 800
>> 800 800 910 910 910 910 910 910 910 910 910 910 910 910 910 910 910 910
>> 910 910 910 910
>> [39] 910 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA
>> [77] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA
>> [115] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA
>> [153] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA
>> [191] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA
>> [229] NA NA NA NA NA NA NA NA NA
>> Warning message:
>> NAs introduced by coercion
>>
>>> as.numeric(levels(mass))[as.integer(mass)]
>>
>> [1] 800 800 800 800 800 800 800 800 800 800 800 800 800 800 800 800
>> 800 800 910 910 910 910 910 910 910 910 910 910 910 910 910 910 910 910
>> 910 910 910 910
>> [39] 910 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA
>> [77] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA
>> [115] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA
>> [153] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA
>> [191] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>> NA NA NA NA
>> [229] NA NA NA NA NA NA NA NA NA
>> Warning message:
>> NAs introduced by coercion
>>
>>> var.matrix$mass <- numeric(var.matrix$mass)
>>
>> Error in "$<-.data.frame"(`*tmp*`, "mass", value = c(0, 0, 0, 0, 0, 0,
>> :
>> replacement has 8 rows, data has 237


R-help@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 Received on Fri May 06 22:58:05 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:37 EST