Errors related to zoo package and dates

440 Views Asked by At

I started getting an error with a data and code that was working fine and now does not. The work is regarding rainfall data and the hydroTSM package which requires zoo. When I get to a part in the code, a simple function included in the hydroTSM package wont run. After troubleshooting, I think it's related to the zoo package but am not 100% sure.

I tried to clean my data using trimws() and use multiple ways to convert my df to a zoo, but nothing. I've started a new R session and rebooted my machine. I also reinstalled HydroTSM. Any idea what it is?

Data

EXAMPLE

Date, Rainfall
2001-01-01  0.00
2001-01-02  0.00
2001-01-03  0.00
2001-01-04  0.00
2001-01-05  0.00
2001-01-06  0.00
2001-01-07  0.00

LINK TO FILE

Code

## Library Packages
library(hydroTSM)
library(devtools)

## Read in the data and format for zoo
df <- read.table("Rainfall2" , header = TRUE, sep="\t")
df$Date <- as.Date(df$Date , format = "%m/%d/%y")
x <- zoo(df[, -1], df[, 1])
dates <- time(x)

## Run the daily2monthly function in HydroTSM
m <- daily2monthly(x, FUN=sum, na.rm=TRUE)

Error in daily2monthly(x, FUN = sum, na.rm = TRUE) : unused arguments (FUN = sum, na.rm = TRUE)

If you look at the link to the package, I am following the directions almost exactly except how I format my data, which I came up with on my own. So I also tried the following code to convert to zoo and got this error.

x <- read.zoo(df, format = "%d/%m/%Y")

Error in read.zoo(df, format = "%d/%m/%Y") : index has 6935 bad entries at data rows:

I also tried and received:

m <- daily2monthly(x)

Error in UseMethod("time<-") : no applicable method for 'time<-' applied to an object of class "zoo"

All the other functions are working in the package including:

nyears <- yip(from=start(x), to=end(x), out.type="nmbr" ) )

smry(x)

hydroplot(x, var.type="Precipitation", main="at Lake Caroline",
          pfreq = "dm", from="2001-01-01")

So, I do not really understand why I am hitting the wall here with daily2monthy()

I could really use your help! Thanks so much!

1

There are 1 best solutions below

0
On

Well, I found a workaround. There are a couple of issues here. One pertaining to my formatting the dates in zoo, and the other having something to do with the library that daily2monthly() resides in.

First thing I did was fix the zoo formatting issue.

FROM: x <- read.zoo(df, format = "%Y-%m-%d")

TO: x <- read.zoo(df, format = "%d/%m/%Y")

That solved the errors with zoo. Then I noticed the library issue when calling the function in the console:

enter image description here

So, I looked up the R documentation and learned you can call subdaily2monthly(), which I tried and it worked. Still not exactly sure the cause, but I think it has something to do with the clashing of libraries assuming daily2monthly() should be part of the hydroTSM group when calling the function from the console.

NEW CODE:

df <- read.table("Rainfall2" , header = TRUE, sep="\t")
df$Date <- as.Date(df$Date , format = "%m/%d/%y")
x <- zoo(df[, -1], df[, 1])
x <- read.zoo(df, format = "%Y-%m-%d")
m <- subdaily2monthly(x, FUN=sum, na.rm=TRUE)

and so on...