Aggregated data by week or several days

I am trying to aggregate a dataframe to get a table with weekly averages of a variable. I found the following package provides a good solution and I use it to aggregate data annually and monthly. However, the weekly data aggregation function simply does not work as described. Anyone have an idea how I can fix this? For example, following the guide:

require(TSAgg)
#Load the data:
data(foo)

##Format the data using the timeSeries function.
foo.ts<-timeSeries(foo[,1],  "%d/%m/%Y  %H:%M",foo[,3])

##Aggregate the data into 6 days blocks using max
(mean.month <- monthsAgg(foo.ts,mean,6))

#Aggregate the data into weeks, using 7 days and mean:
(foo.week<-daysAgg(foo.ts,mean,7) ) 

      

The last command doesn't work. The function is as follows:

daysAgg <-
function (data, process, multiple = NULL, na.rm = FALSE) 
{
    if (is.null(multiple)) {
        multiple = 1
    }
    if (multiple == 1) {
        day <- aggregate(data[, 8:length(data)], list(day = data$day, 
            month = data$month, year = data$year), process, na.rm = na.rm)
        days <- ymd(paste(day$year, day$month, day$day))
        data2 <- data.frame(date = days, data = day[, 4:length(day)])
        names(data2) <- c("Date", names(data[8:length(data)]))
        return(data2)
    }
    temp <- data
    day <- aggregate(list(data[, 8:length(data)], count = 1), 
        list(day = data$day, month = data$month, year = data$year), 
        process, na.rm = na.rm)
    days <- ymd(paste(day$year, day$month, day$day))
    data <- data.frame(date = days, day[, 5:length(day) - 1], 
        count = day[length(day)])
    days = paste(multiple, "days")
    all.dates <- seq.Date(as.Date(data$date[1]), as.Date(data$date[length(data[, 
        1])]), by = "day")
    dates <- data.frame(date = all.dates)
    aggreGated <- merge(dates, data, by = "date", all.x = TRUE)
    aggreGated$date <- rep(seq.Date(as.Date(data$date[1]), as.Date(data$date[length(data[, 
        1])]), by = days), each = multiple, length = length(all.dates))
    results <- aggregate(list(aggreGated[2:length(aggreGated)]), 
        list(date = aggreGated$date), process, na.rm = TRUE)
    results <- subset(results, results$count != 0)
    results <- results[, -length(results)]
    names(results) <- c("Date", names(temp[8:length(temp)]))
    return(results)
}

      

+3


source to share


1 answer


The problem in the code stems from its use of the ymd function, which binds "UTC" to the end of all the dates it outputs. It is possible to overload the function by specifying ymd again using

ymd <- function(x) {
    as.Date(x, "%Y %m %d")  
}

      



before you call daysAgg.

0


source







All Articles