3

I'm trying to aggregate a data frame as to obtain a table with weekly averages of a variable. I found the following package provides a nice solution, and I've been using it for aggregating data yearly and monthly. However, the function to aggregate data weekly simply is not working as described. Does anyone has an idea how I can fix this up? For instance, following the manual:

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 the following:

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)
}
user3407340
  • 93
  • 1
  • 1
  • 4
  • "Why My code is not working" type of questions are OT in SO. If you have a specific question, please provide a reproducible example and your desired output and we will provide you with the verity of answer which you can choose from – David Arenburg Sep 30 '14 at 22:32
  • Well, I just found it quickly to provide the manual's show case. I've tried many times with mine own data till I go to the package's manual and try with its own data, which obviously isn't working either. That was the reason I posted it. In addition, I sent an e-mail for the package developer, so far not a reply. – user3407340 Sep 30 '14 at 22:38
  • does this help? http://stackoverflow.com/questions/4351266/aggregate-weekly-data-in-r – Alex Oct 01 '14 at 00:17
  • `daysAgg(foo.ts,mean)` works. But when `daysAgg()` takes an integer for `multiple`, there is no outcome. No error message. At least, `if (multiple == 1)` in the function should be `if (multiple > 1)`. You may want to communicate with the package author. – jazzurro Oct 01 '14 at 00:19
  • @Alex it does, but I'd seen it before asking. There are many ways I can aggregate data weekly, nonetheless, the output provided by that package was better for the project I doing. Thanks anyway. – user3407340 Oct 01 '14 at 13:58

1 Answers1

0

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

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

before you call daysAgg.