Sum table values ​per day

Viewed 43

I have a table as shown in the image, where each comment has a publication date, with year, month, day and time, I would like to add the sentiment values ​​by day.

this is how the table is composed

serie <- data.frame(comments$created_time,sentiment2$positive-sentiment2$negative)

1 Answers

Using dplyr you can do:

library(dplyr)

df %>% 
  group_by(as.Date(comments.created_time)) %>% 
  summarize(total = sum(sentiment))

Here is some sample data that will help others to troubleshoot and understand the data:

df <- tibble(comments.created_time = c("2015-01-26 22:43:00",
                                       "2015-01-26 22:44:00",
                                       "2015-01-27 22:43:00",
                                       "2015-01-27 22:44:00",
                                       "2015-01-28 22:43:00",
                                       "2015-01-28 22:44:00"),
             sentiment = c(1,3,5,1,9,1))

Using the sample data will yield:

# A tibble: 3 × 2
  `as.Date(comments.created_time)` total
  <date>                           <dbl>
1 2015-01-26                           4
2 2015-01-27                           6
3 2015-01-28                          10
Related