Quantcast

How to convert factors to numbers

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to convert factors to numbers

sandro
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:

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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert factors to numbers

chuck.01
This post was updated on .
Using your posed data, the variable price was numeric:

data.precios <- read.table("http://r.789695.n4.nabble.com/file/n4498828/p_diarios.txt", header=T)

str(data.precios)
'data.frame': 1996 obs. of  2 variables:
 $ time : int  37988 37991 37993 37994 37995 37998 37999 38000 38001 38002 ...
 $ price: num  18.1 26.1 30.9 34.7 27.6 ...


HOWEVER!
If I follow your code (eg. using " read.table(... , dec=",", sep="\t")":

 data.precios <- read.table("http://r.789695.n4.nabble.com/file/n4498828/p_diarios.txt",  header =TRUE , dec=",", sep="\t")
> str(data.precios)
'data.frame': 1996 obs. of  2 variables:
 $ time : int  37988 37991 37993 37994 37995 37998 37999 38000 38001 38002 ...
 $ price: Factor w/ 1639 levels "10.80","12.53",..: 12 126 213 342 160 186 219 37 54 69 ...

It is a factor.... but I can change it like this:

data.precios$price <- as.numeric(as.character(data.precios$price))

> str(data.precios)
'data.frame': 1996 obs. of  2 variables:
 $ time : int  37988 37991 37993 37994 37995 37998 37999 38000 38001 38002 ...
 $ price: num  18.1 26.1 30.9 34.7 27.6 ...


I think avoiding it ever becoming a factor would be the better path.
Good luck.




sandro 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:

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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert factors to numbers

Natasha Stavros
In reply to this post by sandro
As.numeric(as.character(factor.level.to.convert))
On Mar 23, 2012 11:40 AM, "sandro" <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert factors to numbers

Uwe Ligges-3
In reply to this post by chuck.01


On 23.03.2012 15:31, chuck.01 wrote:

> Using your posed data, the variable price was numeric:
>
> data.precios<-
> read.table("http://r.789695.n4.nabble.com/file/n4498828/p_diarios.txt",
> header=T)
>
> str(data.precios)
> 'data.frame': 1996 obs. of  2 variables:
>   $ time : int  37988 37991 37993 37994 37995 37998 37999 38000 38001 38002
> ...
>   $ price: num  18.1 26.1 30.9 34.7 27.6 ...
>
>
> HOWEVER!
> If I follow your code (eg. using " read.table(... , dec=",", sep="\t")":
>
>   data.precios<-
> read.table("http://r.789695.n4.nabble.com/file/n4498828/p_diarios.txt",
> header =TRUE , dec=",", sep="\t")
>> str(data.precios)
> 'data.frame': 1996 obs. of  2 variables:
>   $ time : int  37988 37991 37993 37994 37995 37998 37999 38000 38001 38002
> ...
>   $ price: Factor w/ 1639 levels "10.80","12.53",..: 12 126 213 342 160 186
> 219 37 54 69 ...
>
> It is a factor.... but I can change it like this:
>
>> Price<- as.numeric(data.precios$price)

Never ever! It will convertwed to the intergers representing the former
factors. You probbaly want as.numeric(as.character(...)) and have to
look why R has not read it as a numeric right away.

Uwe Ligges





>> str(lPrice)
>   num [1:1996] 12 126 213 342 160 186 219 37 54 69 ...
>
>
> I think avoiding it ever becoming a factor would be the better path.
> Good luck.
>
>
>
>
>
> sandro 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-tp4498828p4499019.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert factors to numbers

David Winsemius
In reply to this post by sandro

On Mar 23, 2012, at 9:38 AM, sandro 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")

Why are you using dec="," when you have decimal points in the file?


> 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.

You could read the FAQ on this matter ... number 7.21 or in that  
general vicinity. But it would be easier to fix the error in your  
input statement.

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert factors to numbers

Rainer Schuermann
In reply to this post by Natasha Stavros
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"<[hidden email]>  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.
>>
>> ______________________________________________
>> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert factors to numbers

sandro
In reply to this post by sandro
 Thanks Natasha, Rainer and David. Your support allow me to solve the issue with the factor number.

Sincerely

Sandro
Loading...