dplyr count observations per day

吃可爱长大的小学妹 提交于 2019-12-02 08:14:17

问题


I have the following data

Name         Date                                   Message
Ted Foe      2011-06-10T05:06:30+0000               I love this product
Sina Fall    2011-06-10T05:07:33+0000               Not my type of product
Steve Hoe    2011-06-11T05:06:30+0000               Great Discussion! Thanks
Selda Dee    2011-06-13T05:12:30+0000               Seen elsewhere
Steven Hoe   2011-06-13T03:17:31+0000               Where?
Selda Dee    2011-06-13T05:17:56+0000               Tinder

I want to aggregate by days so that I end up with a time series like this

Date            Number of Posts
2011-06-10      2
2011-06-11      1
2011-06-12      0
2011-06-13      3

I already tried the following

summary_df <- df %>% group_by(Date) %>% summarise(comments = count(message))

But this is not working. Any quick dplyr based solution would be great.

Thanks for the help!

Cheers, Raoul


回答1:


Grouped by the 'Date' column after converting to Date class, we get the number of rows (n()) with summarise. If we need the 'Date' elements that are missing in the original dataset, create a new dataset with the sequence of minimum to maximum 'Date' and do a left_join

df1 <- df %>%
          group_by(Date = as.Date(Date)) %>%
          summarise(comments = n())
expand.grid(Date = seq(min(df1$Date), max(df1$Date), by = '1 day')) %>%
         left_join(., df1)


来源:https://stackoverflow.com/questions/36549087/dplyr-count-observations-per-day

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!