[R] Help with read.zoo and transform

Gabor Grothendieck ggrothendieck at gmail.com
Mon Dec 24 14:58:09 CET 2007


zoo objects are intended to represent time series and are
based on vectors and matrices, like ts objects, not data frames.
See ?zoo

Create your new columns as numeric variables:

> library(zoo)
> library(chron) # need chron 2.3-16 for month.day.year
> z <- zoo(matrix(1:24, 6, 4), Sys.Date() + seq(0, length = 6, by = 32)) # test data

> # add year, month and day as columns to z
> with(month.day.year(time(z)), cbind(z, year, month, day, quarter = (month - 1) %/% 3 + 1, dow = as.numeric(format(time(z), "%w"))))
           z.1 z.2 z.3 z.4 year month day quarter dow
2007-12-24   1   7  13  19 2007    12  24       4   1
2008-01-25   2   8  14  20 2008     1  25       1   5
2008-02-26   3   9  15  21 2008     2  26       1   2
2008-03-29   4  10  16  22 2008     3  29       1   6
2008-04-30   5  11  17  23 2008     4  30       2   3
2008-06-01   6  12  18  24 2008     6   1       2   0





On Dec 24, 2007 8:30 AM, Alex Park <alex.park1 at ntlworld.com> wrote:
> 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
>
> ______________________________________________
> 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