changing POSIXct date vaules to first day of each week

穿精又带淫゛_ 提交于 2019-12-05 07:13:05

Using bosom buddy of plyr,

library(lubridate)
library(dplyr)
df %>% 
  group_by(Week = floor_date(Date, unit="week")) %>% 
  summarize(WeeklyAveDist=mean(Dist))
#Source: local data frame [3 x 2]
#
#        Week WeeklyAveDist
#1 2012-02-12      381.7755
#2 2012-02-19      252.1116
#3 2012-02-26      175.4097

There are also ceiling_date, round_date options.

You could use strftime with %W format:

> strftime(as.Date("2015-01-08"), "%W")
[1] "01"

You could use this to define a new variable and then aggregate by this variable. Maybe thus

> df <- transform(df, week=strftime(Date, "%W"))
> aggregate(df$Dist, by=list(df$week), FUN=mean)
  Group.1        x
1      07 319.8861
2      08 254.2861
3      09 161.0421
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!