0

I am trying use hydroplot from package hydroTSM, but i get the following error:

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

I have used this code:

    td<-seq(as.Date("2009/02/12"), as.Date("2013/07/11"), "days") 
    Tempzoo = zoo(x=Temp, order.by=td)

    hydroplot(Tempzoo, na.rm=TRUE, ptype="ts+boxplot+hist", pfreq="dma",                      
      var.type="Temperature", var.unit="units", main=NULL, xlab="Time", ylab="Temp",
      win.len1=0, win.len2=0, tick.tstep="auto", lab.tstep="auto", 
      lab.fmt=NULL, cex=0.3, cex.main=1.3, cex.lab=1.3, cex.axis=1.3, 
      col=c("blue", "lightblue", "lightblue"), stype="default", season.names=c("Winter", "Spring", "Summer", "Autumn"), 
      h=NULL)

Below is a subset of my data set:

    2009-02-12 2009-02-13 2009-02-14 2009-02-15 2009-02-16 2009-02-17 2009-02-18 
      3.535849   3.505208   3.973958   4.398958   4.004167   4.175000   5.598958 
    2009-02-19 2009-02-20 2009-02-21 2009-02-22 2009-02-23 2009-02-24 2009-02-25 
      5.507292   5.245833   5.639583   5.988542   6.458333   6.567708   5.811458 
    2009-02-26 2009-02-27 2009-02-28 2009-03-01 2009-03-02 2009-03-03 2009-03-04 
      5.533333   5.976042   6.027083   6.112500   5.576042   5.176042   5.326042 
    2009-03-05 2009-03-06 2009-03-07 2009-03-08 2009-03-09 2009-03-10 2009-03-11 
      4.746875   4.485417   4.975000   4.960417   4.829167   4.942708   5.175000 
    2009-03-12 2009-03-13 2009-03-14 2009-03-15 2009-03-16 2009-03-17 2009-03-18 
      5.789583   6.137500   6.628125   6.755208   7.046875   6.864583   7.033333 
    2009-03-19 2009-03-20 2009-03-21 2009-03-22 2009-03-23 2009-03-24 2009-03-25 
      7.427083   7.191667   7.229167   6.740625   6.194792         NA   6.117708 
    2009-03-26 2009-03-27 2009-03-28 2009-03-29 2009-03-30 2009-03-31 2009-04-01 
      6.702083   6.268750         NA   5.797917   6.637500   7.918750   8.744792 
    2009-04-02 2009-04-03 2009-04-04 2009-04-05 2009-04-06 2009-04-07 2009-04-08 
            NA         NA         NA   7.330208   7.075000         NA         NA 
    2009-04-09 2009-04-10 2009-04-11 2009-04-12 2009-04-13 2009-04-14 2009-04-15 
            NA         NA         NA         NA   9.031250   8.687500   8.841667 
    2009-04-16 2009-04-17 2009-04-18 2009-04-19 2009-04-20 2009-04-21 2009-04-22 
      9.947917   8.946875   8.512500   9.478125         NA  10.075000         NA 
   2009-04-23 2009-04-24 2009-04-25 2009-04-26 2009-04-27 2009-04-28 2009-04-29 
            NA         NA   9.272917   9.511458         NA   9.295833   9.764583 

Any ideas on how to resolve this? Thanks!

Tung
  • 26,371
  • 7
  • 91
  • 115
Jesinsky
  • 71
  • 2
  • 9
  • 1
    [Search on SO for your error message](http://stackoverflow.com/search?q=[r]+Error+in+UseMethod%28%22time%3C-%22%29+%3A+no+applicable+method+for+%27time%3C-%27+applied+to+an+object+of+class+%22zoo%22), try `detach('package:timeSeries')`, try `hydroplot` again. And next time, please make it easier for people that wish to help you by providing [example data in a convenient format](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Thanks! – Henrik Oct 03 '13 at 16:21
  • @Henrik: Sounds like your comments taken together qualify as an answer. – IRTFM Oct 03 '13 at 16:51
  • It's somewhat ironic that the "possible" duplicate was closed because it was thought unlikely to help future readers, and here we have an instance where the future user might get help and the answer appears useful. I also think the original could be improved to unload a namespace correctly, but you obviously cannot go back and fix ... because it's closed. No one should dare to vote to close now! – IRTFM Oct 03 '13 at 17:01
  • @DWin, since I am not `detach`ing on a regular basis, please suggest an edit in my post on how to "unload a namespace correctly". In my answer I just used the first `?detach` example as template. – Henrik Oct 03 '13 at 17:15

1 Answers1

2

I searched SO for your error message, and tried the solution provided by @Roland. And yes, it seems like package timeSeries was loaded when you tried to plot. Here is an example:

library(zoo)
library(hydroTSM)

# create data
td <- seq(as.Date("2009/02/12"), as.Date("2013/07/11"), "days") 
n <- length(td)
Temp <- sample(c(rnorm(n, mean = 10), rep(NA, n/5)), n)
Tempzoo <- zoo(x = Temp, order.by = td)


# in addition, load the suspected package ‘timeSeries’ 
library(timeSeries)
# Attaching package: ‘timeSeries’
# 
# The following object is masked from ‘package:zoo’:
#   
#   time<-

# try plot
hydroplot(x = Tempzoo,
          na.rm = TRUE,
          ptype = "ts+boxplot+hist",
          pfreq = "dma",                      
          var.type = "Temperature",
          var.unit = "units",
          main = NULL,
          xlab = "Time", ylab = "Temp",
          win.len1 = 0, win.len2 = 0,
          tick.tstep = "auto", lab.tstep = "auto", 
          lab.fmt = NULL,
          cex = 0.3, cex.main = 1.3, cex.lab = 1.3, cex.axis = 1.3, 
          col=c("blue", "lightblue", "lightblue"), stype="default",
          season.names = c("Winter", "Spring", "Summer", "Autumn"), 
          h=NULL)

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

# i.e. your error

# detach ‘timeSeries’ 
detach(package:timeSeries)

# plot again

enter image description here

Community
  • 1
  • 1
Henrik
  • 65,555
  • 14
  • 143
  • 159
  • My understanding is that `unloadNamespace(ns)` is safer than `detach`. It also will detach the namespace. – IRTFM Oct 03 '13 at 17:18