[R] Help with read.zoo and transform
Alex Park
alex.park1 at ntlworld.com
Mon Dec 24 14:30:44 CET 2007
R
I get a daily feed of data over the internet that I keep in various .csv
files.
I have built a function that reads that data into R for me:
getMarketData<-function(market)
{
library(zoo)
pathname<- "C:/DATA/"
files<-c("AN_REV.csv","AX_REV.csv","BN_REV.csv")
markets<-c("AUS","DAX","GBP")
df<-read.zoo(paste(pathname,files[match(market,markets)],sep=""),
index.column=1, format="%m/%d/%Y", header=F, sep=",")
df
}
This works fine and returns the dataset as planned:
V2 V3 V4 V5 V6 V7
1990-01-02 54.89 54.99 54.61 54.61 125 2576
1990-01-03 54.48 54.62 54.29 54.33 1495 3232
1990-01-04 54.67 55.20 54.59 55.08 932 3145
1990-01-05 54.64 54.87 54.57 54.57 272 2567
1990-01-08 54.87 54.89 54.68 54.79 177 2456
1990-01-09 54.87 54.96 54.80 54.88 106 2403
Also if I type the following:
mode(df[1,1])
>"numeric"
That is, my data is treated as numeric (which is as it should be).
Here is where I get a problem. I'd like to add some more columns on to the
end of the dataset to represent the year, quarter, month, and day.
If I try the following within the function it won't work:
df<-transform(df, "Year"=format(time(df), "%Y"))
Curiously, if I do exactly the same line by line in R (i.e. without
specifying a function) then it works fine. Is there any reason why I cannot
use transform in a function?
I also tried a different approach as shown below i.e within my original
function I added the following lines after I had created df:
df[,8:11]<-0
df[,7]<-format(time(df), format="%Y")
df[,8]<-quarters(time(df))
df[,9]<-months(time(df))
df[,10]<-weekdays(time(df))
This worked fine however it changed all my data from numeric to character:
Open High Low Close Volume OI Year Quarter Month Day
1990-01-02 54.89 54.99 54.61 54.61 125 2576 1990 Q1 January Tuesday
1990-01-03 54.48 54.62 54.29 54.33 1495 3232 1990 Q1 January Wedesday
1990-01-04 54.67 55.2 54.59 55.08 932 3145 1990 Q1 January Thursday
1990-01-05 54.64 54.87 54.57 54.57 272 2567 1990 Q1 January Friday
1990-01-08 54.87 54.89 54.68 54.79 177 2456 1990 Q1 January Monday
1990-01-09 54.87 54.96 54.8 54.88 106 2403 1990 Q1 January Tuesday
mode(df[1,1])
>"character"
Why does my numeric data get changed into character?
Can anybody see a simple way to add the data I require whilst retaining the
"numeric" format?
Regards
Alex Park
More information about the R-help
mailing list