将每日数据汇总到月/年间隔

Bti*_*rt3 55 datetime r

我不经常在R中使用日期,但我想这很容易.我有一个代表数据框中日期的列.我只想创建一个新的数据框,使用日期按月/年汇总第二列.什么是最好的方法?

我想要第二个数据帧,以便将其提供给绘图.

您将提供的任何帮助将不胜感激!

编辑:供参考:

> str(temp)
'data.frame':   215746 obs. of  2 variables:
 $ date  : POSIXct, format: "2011-02-01" "2011-02-01" "2011-02-01" ...
 $ amount: num  1.67 83.55 24.4 21.99 98.88 ...

> head(temp)
        date amount
1 2011-02-01  1.670
2 2011-02-01 83.550
3 2011-02-01 24.400
4 2011-02-01 21.990
5 2011-02-03 98.882
6 2011-02-03 24.900
Run Code Online (Sandbox Code Playgroud)

had*_*ley 50

我会这样做,lubridate并将plyr日期舍入到最接近的月份,以便更容易绘制:

library(lubridate)
df <- data.frame(
  date = today() + days(1:300),
  x = runif(300)
)
df$my <- floor_date(df$date, "month")

library(plyr)
ddply(df, "my", summarise, x = mean(x))
Run Code Online (Sandbox Code Playgroud)

  • 或者使用dplyr,最后一行是`summary(df,x = mean(my))`. (4认同)

kmm*_*kmm 37

可能有一个更优雅的解决方案,但分为几个月和几年,strftime()然后aggregate()应该这样做.然后重新组装绘图日期.

x <- as.POSIXct(c("2011-02-01", "2011-02-01", "2011-02-01"))
mo <- strftime(x, "%m")
yr <- strftime(x, "%Y")
amt <- runif(3)
dd <- data.frame(mo, yr, amt)

dd.agg <- aggregate(amt ~ mo + yr, dd, FUN = sum)
dd.agg$date <- as.POSIXct(paste(dd.agg$yr, dd.agg$mo, "01", sep = "-"))
Run Code Online (Sandbox Code Playgroud)


Jaa*_*aap 17

游戏有点晚,但另一种选择是使用data.table:

library(data.table)
setDT(temp)[, .(mn_amt = mean(amount)), by = .(yr = year(date), mon = months(date))]

# or if you want to apply the 'mean' function to several columns:
# setDT(temp)[, lapply(.SD, mean), by=.(year(date), month(date))]
Run Code Online (Sandbox Code Playgroud)

这给了:

     yr      mon mn_amt
1: 2011 februari 42.610
2: 2011    maart 23.195
3: 2011    april 61.891
Run Code Online (Sandbox Code Playgroud)

如果你想要几个月的名字而不是数字,你可以使用:

setDT(temp)[, date := as.IDate(date)
            ][, .(mn_amt = mean(amount)), by = .(yr = year(date), mon = months(date))]
Run Code Online (Sandbox Code Playgroud)

这给了:

     yr      mon mn_amt
1: 2011 februari 42.610
2: 2011    maart 23.195
3: 2011    april 61.891
Run Code Online (Sandbox Code Playgroud)

如您所见,这将以您的系统语言(在我的情况下为荷兰语)中给出月份名称.


或使用的组合lubridatedplyr:

temp %>% 
  group_by(yr = year(date), mon = month(date)) %>% 
  summarise(mn_amt = mean(amount))
Run Code Online (Sandbox Code Playgroud)

使用数据:

# example data (modified the OP's data a bit)
temp <- structure(list(date = structure(1:6, .Label = c("2011-02-01", "2011-02-02", "2011-03-03", "2011-03-04", "2011-04-05", "2011-04-06"), class = "factor"), 
                       amount = c(1.67, 83.55, 24.4, 21.99, 98.882, 24.9)), 
                  .Names = c("date", "amount"), class = c("data.frame"), row.names = c(NA, -6L))
Run Code Online (Sandbox Code Playgroud)


Gal*_*ich 8

你可以这样做:

short.date = strftime(temp$date, "%Y/%m")
aggr.stat = aggregate(temp$amount ~ short.date, FUN = sum)
Run Code Online (Sandbox Code Playgroud)


Gal*_*ich 8

只需使用xts包就可以了.

library(xts)
ts <- xts(temp$amount, as.Date(temp$date, "%Y-%m-%d"))

# convert daily data
ts_m = apply.monthly(ts, FUN)
ts_y = apply.yearly(ts, FUN)
ts_q = apply.quarterly(ts, FUN)
Run Code Online (Sandbox Code Playgroud)

其中FUN是一个汇总数据的函数(例如sum)


sbh*_*bha 5

这是一个dplyr选项:

library(dplyr)

df %>% 
  mutate(date = as.Date(date)) %>% 
  mutate(ym = format(date, '%Y-%m')) %>% 
  group_by(ym) %>% 
  summarize(ym_mean = mean(x))
Run Code Online (Sandbox Code Playgroud)