[R] Convert data frame entries to numbers

Peter Ehlers ehlers at ucalgary.ca
Mon Oct 19 16:06:36 CEST 2009


joris meys wrote:
> 2 problems :
> test seems to be a data frame or list with one variable. So you have
> to specify :
> as.numeric(test$classcol)
> 
> But this will make the internal factor levels the real values, not the
> numbers you specified. What you need, is
> 
> as.numeric(as.character(test$classcol))

which is prominently mentioned in ?factor as well as in
7.10 of the FAQ.

  -Peter Ehlers

> 
> Cheers
> Joris
> 
> On Mon, Oct 19, 2009 at 3:07 PM, John <josip.2000 at gmail.com> wrote:
>> Hello,
>>
>> I figure this is quite a simple problem really, but since I'm quite
>> new to R I need to ask:
>>
>> If I have a data frame:
>>
>> str(test) :
>> $ classcol: Factor w/ 3 levels "1","2","3"
>>
>> How do I convert the entries in this column to numbers. I.e., I want
>> to be abl to do simple calculations like test[1,the_column] - test
>> [2,the_column] without errors.
>>
>>
>> as.numeric(test) yeilds the error "object cannot be coerced to type
>> 'double'"
>>
>>
>> Thanks!
>> John
>>
>> ______________________________________________
>> 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.
>>
> 
> ______________________________________________
> 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.
> 
>




More information about the R-help mailing list