Pergunta

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?

Foi útil?

Solução

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)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top