0

I am trying to understand from this example if lubridate can be applied to apply.daily but don't quite understand how to do that. can I get any reference to using lubridate with apply.daily so I can exclude weekends using apply.daily

Stackoverflow: Wrong week-ending date using 'to.weekly' function in 'xts' package

EDIT: using Richie Cotton's example as a guide, I wrote the following:

> is.weekend <- function(x) {

+    w <- as.POSIXlt(x)

+    w %in% c(1,7)

+ }

> apply.daily(core[!is.weekend(x)],dfun)

Error in as.POSIXlt.default(x) :

  do not know how to convert 'x' to class "POSIXlt"

> apply.daily(core[!is.weekend(index(x))],dfun)

Error in as.POSIXlt.numeric(x) : 'origin' must be supplied

> alpha <- core[!is.weekend(index(x))]

Error in as.POSIXlt.numeric(x) : 'origin' must be supplied

> 

Where is my error? Am I missing a particular library?

Community
  • 1
  • 1
user1155299
  • 877
  • 5
  • 20
  • 29
  • What is the variable `x` that you are passing in to `is.weekend`? Whatever it is, it can't be converted to a `POSIXlt`. Also, checking that an object of class `POSIXlt` is equal to 1 or 7 won't work. You need to check `w$wday %in% c(0, 6)` or use the `lubridate::wday` function as I recommended. – Richie Cotton Jan 30 '12 at 16:27

1 Answers1

2

Create your time series.

x <- today() + hours(0:(24 * 14))
time_series <- xts(rnorm(length(x)), x)

Write a function to check whether a date occurs on a weekend.

is.weekend <- function(x)
{
  day_of_week <- wday(x, label = TRUE)
  day_of_week %in% c("Sat", "Sun")
}

Exclude those from your call to apply.daily.

apply.daily(time_series[!is.weekend(x)], max)
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360