[R] how to convert factors to numbers

doloop nbl1 at uslex.net
Sun Dec 14 18:38:20 CET 2008

Hello, I am relatively new to using R.  I am using R version 2.8.0.  I have a
program that downloads stock data from Yahoo! Finance and stores it to a
text file on my hard drive.  The text file contains the date, opening price,
high price, low price, closing price, volume and adjusted price (i.e.,
adjusted for dividends and splits).  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:

> spy<-read.table("c:\\StockData\\SPY.txt")
> attach(spy)
> names(spy)<-c("QDate","OpenP","HighP","LowP","CloseP","Vol","AdjP")
> spy[1,]
        QDate   OpenP   HighP    LowP  CloseP       Vol   AdjP
1 12/14/2006, 141.86, 143.24, 141.84, 143.12, 64755200, 138.34
> ChangeFromOpen<-spy[1,5]-spy[1,2]
Warning message:
In Ops.factor(spy[1, 5], spy[1, 2]) : - not meaningful for factors
As you can see, I cannot calculate the difference between the closing price
and the opening price, (much less compute averages, etc).  This is clearly a
"newbie" problem.  In my defense, I am using the book by Michael Crawley
(The R Book) as my teaching guide, but I cannot find the answer to this
question in that rather densely packed book.

Any help is appreciated.
View this message in context: http://www.nabble.com/how-to-convert-factors-to-numbers-tp21002508p21002508.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list