按周或几天汇总数据

时间:2014-09-30 22:28:08

标签: r

我正在尝试聚合数据框以获取具有变量的每周平均值的表。我发现以下软件包提供了一个很好的解决方案,我一直用它来每年和每月汇总数据。但是,每周汇总数据的功能根本无法解释。有谁知道如何解决这个问题?  例如,遵循手册:

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) ) 

最后一个命令不起作用。功能如下:

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)
}

1 个答案:

答案 0 :(得分:0)

代码中的问题源于它使用函数ymd,它附加了&#34; UTC&#34;到它输出的所有日期的结尾。通过使用

再次定义ymd可以使函数重载
ymd <- function(x) {
    as.Date(x, "%Y %m %d")  
}

在你打电话给daysAgg之前。