[R] factor -> numeric
Spencer Graves
spencer.graves at pdf.com
Wed Jun 2 21:50:33 CEST 2004
Have you tried "as.numeric(as.character(rsm2$Value))"? The
construct "as.numeric(rsm2$Value)" returns the NUMERIC CODES for the
different levels; "as.character(rsm2$Value)" returns the character
representation, which you can then convert to numeric.
hope this helps. spencer graves
Mikkel Grum wrote:
>Hi,
>
>I'm extracting data from a database with values for
>different observation types in the same variable
>(another variable deifnes the observation type). Some
>of these observation types are factors, so R naturally
>classifies the entire variable as a factor. I want to
>select a subset and convert the values to numeric
>values, but it isn't working; as shown below:
>
>
>
>>rsm2<-subset(rsm,PlantStrataId==2,select=c(Value))
>>rsm2$Value
>>
>>
> [1] 70 30
> 15 50
> [26] 2
>
> [51]
>6
> [76]
> 30 10
>[101] 17
> 15
>[126]
>24 Levels: 0-25 1 10 12 13 15 17 2 20 23 25 25-50 3
>30 4 40 5 50 50-75 ... Present
>
>
>>rsm2$Value<-as.numeric("rsm2$Value")
>>
>>
>Warning message:
>NAs introduced by coercion
>
>
>>rsm2$Value
>>
>>
> [1] 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
> [26] 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
> [51] 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
> [76] 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
>[101] 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
>[126] NA
>
>
>
>is there any way out? Help much appreciated.
>
>cheers,
>Mikkel
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>
More information about the R-help
mailing list