简体   繁体   English

在 R 中聚合每周数据

[英]Aggregate Weekly Data in R

I am sure this is straight forward but I just cant seem to get it to work.我确信这是直接的,但我似乎无法让它发挥作用。 I have a data frame that represents daily totals.我有一个代表每日总数的数据框。 I simply want to sum the totals by week, retaining a zero if a week is not represented.我只想按周对总数求和,如果没有表示一周,则保留零。 What is the best approach in R? R中的最佳方法是什么? In case it matters, I read the data in from a CSV and converted it to a date once in R.万一重要,我从 CSV 读取数据并将其转换为 R 中的日期。

Here is the structure of my data frame p1:这是我的数据框 p1 的结构:

'data.frame':   407 obs. of  2 variables:
 $ date:Class 'Date'  num [1:407] 14335 14336 14337 14340 14341 ...
 $ amt : num  45 150 165 165 45 45 150 150 15 165 ...

and the first few...和前几个...

> head(p1)
        date amt
1 2009-04-01  45
2 2009-04-02 150
3 2009-04-03 165
4 2009-04-06 165
5 2009-04-07  45
6 2009-04-08  45

Many thanks in advance.提前谢谢了。

One note: I saw one previous post but couldn't get it to work一个注意事项:我看到了以前一篇文章,但无法让它工作

A solution with the lubridate library: lubridate库的解决方案:

library(lubridate)
Lines <- "date,amt
2009-04-01,45
2009-04-02,150
2009-04-03,165
2009-04-13,165
2009-04-14,45
2009-04-15,45
2009-05-15,45"
df <- read.csv(textConnection(Lines))

If you don't need 0 for missing weeks it's simple:如果您不需要 0 丢失的周数,这很简单:

weeks <- week(df$date)
sums <- tapply(df$amt, weeks, sum)
# 14  15  16  20 
#360 210  45  45 

To put zeros for missing weeks:将缺失的周数归零:

span <- min(weeks):max(weeks)
out <- array(0, dim = length(span), dimnames = list(span))
out[dimnames(sums)[[1]]] <- sums
# 14  15  16  17  18  19  20 
#360 210  45   0   0   0  45 

Here is a solution that reads in the data, aggregates it by week and then fills in missing weeks with zero all in 3 lines of code.这是一个读入数据,按周聚合,然后在 3 行代码中用零填充缺失的周的解决方案。 read.zoo reads it in assuming a header and a field separator of comma. read.zoo在假设标题和逗号的字段分隔符中读取它。 It converts the first column to Date class and then transforms the date to the following Friday.它将第一列转换为Date类,然后将日期转换为下一个星期五。 The nextfri function that does this transformation taken from the zoo-quickref vignette in the zoo package.执行此转换的nextfri函数取自 zoo 包中的zoo-quickref小插图。 (If you want to have the end of week be a different day of the week just replace every 5 in the formula with another day number. The idea is that relative to the UNIX Epoch that d-4 falls on day of the week d where d=0 is Sunday, d=1 is Monda, ..., d=6 is Saturday so any multiple of 7 days from that also falls on day of the week d.) The read.zoo command also aggregates all points that have the same index (remember that we have transformed them to the last Friday of the week so all points in the same week will have the same Friday as their index now). (如果你想让周末成为一周中的不同一天,只需将公式中的5 替换为另一个天数。这个想法是,相对于 UNIX Epoch,d-4 落在一周中的某一天 d 在哪里d=0 是星期日,d=1 是蒙达,...,d=6 是星期六,所以任何 7 天的倍数也属于星期几 d。) read.zoo命令还聚合所有具有相同的指数(请记住,我们已将它们转换为一周的最后一个星期五,因此同一周中的所有点将与现在的指数具有相同的星期五)。 The next command creates a zero width zoo object that has the weeks from the first to the last and merges that with the output of the read using fill = 0 so that the filled in weeks get that value.下一个命令创建一个零宽度动物园对象,该对象具有从第一个到最后一个的周数,并使用fill = 0将其与读取的输出合并,以便填充的周数获得该值。

Lines <- "date,amt
2009-04-01,45
2009-04-02,150
2009-04-03,165
2009-04-13,165
2009-04-14,45
2009-04-15,45"
library(zoo)
nextfri <- function(x) 7 * ceiling(as.numeric(x - 5 + 4)/7) + as.Date(5 - 4)
z <- read.zoo(textConnection(Lines), header = TRUE, sep = ",", 
    FUN = as.Date, FUN2 = nextfri, aggregate = sum)
merge(z, zoo(, seq(min(time(z)), max(time(z)), 7)), fill = 0)

We used textConnection(Lines) above to make it self contained so that you can just copy this and paste it right into your session but in reality textConnection(Lines) would be replaced with the name of your file, eg "myfile.csv" .我们使用上面的textConnection(Lines)使其自包含,这样您就可以复制它并将其直接粘贴到您的会话中,但实际上textConnection(Lines)将替换为您的文件名,例如"myfile.csv"

For the input above the output would be the following zoo object:对于上面的输入,输出将是以下动物园对象:

2009-04-03 2009-04-10 2009-04-17 
       360          0        255

There are three vignettes that come with the zoo package that you might want to read.您可能想阅读 zoo 软件包附带的三个小插图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM