Gekko
Gekko

Reputation: 5

Error on loading zoo library

I'm trying to use the zoo package but unfortunately it's showing an error when I try use it.

As a matter of fact, the results that I'm getting are wrong in the last line.

> library(zoo)

Attaching package: ‘zoo’

The following object(s) are masked from ‘package:base’:

    as.Date, as.Date.numeric

> library(zoo)
> sbux=read.csv(file="sbuxPrices.csv",header=TRUE)
> dates.sbx = as.yearmon(sbux.df$Date, format="%m/%d/%Y")
> sbux.z=zoo(x=sbux$Adj.Close, order.by=dates.sbx)
> class(sbux.z)
[1] "zoo"
> head(sbux.z)
Jan 0000 Feb 0000 Mar 0000 Apr 0000 May 0000 Jun 0000 
    7.66     8.41    10.73     7.24     8.14     9.14 

EDIT: (1)My data is http://faculty.washington.edu/ezivot/econ424/sbuxPrices.csv and believe there is no problem with it as my lecture note works with it and get the right results. (2) Removed the rstudio tag. I thought that the fact that I'm coding in it made any difference!

Thank you!

Upvotes: 0

Views: 278

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368647

Briefly:

  1. You don't show the data, I suspect your format string is simply wrong.

  2. Please re-read the detailed an thorough vignettes in the zoo package.

Edit: Now that you made this reproducible by supplying data:

R> sbux <- read.csv("http://faculty.washington.edu/ezivot/econ424/sbuxPrices.csv",
+                   stringsAsFactors=FALSE)
R> summary(sbux)
     Date             Adj.Close    
 Length:181         Min.   : 1.19  
 Class :character   1st Qu.: 3.94  
 Mode  :character   Median : 9.23  
                    Mean   :12.38  
                    3rd Qu.:18.93  
                    Max.   :37.76  
R> 
R> 
R> sbuxZoo <- zoo(sbux[,"Adj.Close"], 
+                 order.by=as.Date(sbux[,"Date"], "%d/%m/%Y"))
R> 
R> head(sbuxZoo)
1993-01-04 1993-01-06 1993-01-07 1993-01-09 1993-01-10 1993-01-11 
      1.21       1.53       1.48       1.71       1.67       1.39 
R> 

Upvotes: 1

Related Questions