[R] How to convert factors to numbers

Rainer Schuermann Rainer.Schuermann at gmx.net
Sat Mar 24 08:43:08 CET 2012


I guess the problem starts with setting read.table(...dec = ",", ... ). 
The data in your file are with decimal point.

Without that, it works just fine:

 > f <- "http://r.789695.n4.nabble.com/file/n4498828/p_diarios.txt"
 > df <- read.table( f, header = TRUE )
 > log.Price <- log(df$price)
 > head( log.Price )
[1] 2.893700 3.262701 3.432373 3.545586 3.317816 3.387098

Rgds,
Rainer



On 2012-03-23 19:47, Natasha Stavros wrote:
> As.numeric(as.character(factor.level.to.convert))
> On Mar 23, 2012 11:40 AM, "sandro"<sonavarr at gmail.com>  wrote:
>
>> Hello, I am relatively new to using R.
>>
>> The text file contains the date and price .  I want to read and manipulate
>> the data in R.  However, when I use read.table, it treats all of the data
>> as
>> "factors" and I do not know how to treat the data as numbers:
>>
>> http://r.789695.n4.nabble.com/file/n4498828/p_diarios.txt p_diarios.txt
>>
>> setwd ("C:\\Users\\Sandro\\Dropbox\\R")
>> data.precios<- read.table ("p_diarios.txt ", header =TRUE
>> , dec=",", sep="\t")
>> Time<- data.precios$time # 01.02.2004 - 12.05.2011
>> Price<- data.precios$price # Historical spot price
>> log.Price<- log(data.precios$price)
>> Error en Math.factor(c(12L, 126L, 213L, 342L, 160L, 186L, 219L, 37L, 54L,
>>   :
>>   log not meaningful for factors
>>
>> As you can see, I cannot calculate the price logarithms.
>>
>> Any help is appreciated.
>>
>> Sandro
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/How-to-convert-factors-to-numbers-
tp4498828p4498828.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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