0

I have this datatable:

date, sentiment, price
2015-09-05, 1, 200
2015-09-05, 2, 200
2015-09-06, 1, 300

I would like to get a new datatable with the sum of the sentiment per day, keeping the daily price

I tried to do this:

new_dt <- dt%>%
  select(date, sentiment, price)%>%
  filter(date > "2015-09-05" & date <"2015-09-06")
  group_by(date)

expected output:

date, sentiment, price
2015-09-05, 3, 200
2015-09-06, 1, 300
Frank
  • 65,012
  • 8
  • 95
  • 173
Pablo Picciau
  • 410
  • 2
  • 10
  • 4
    After the `group_by(date) %>% summarise(price = first(price), sentiment = sum(sentiment))` – akrun Jun 04 '19 at 15:08

1 Answers1

2

Use the summarise() function to get summary statistics:

new_dt <- dt%>%
  select(date, sentiment, price)%>%
  filter(date > "2015-09-05" & date <"2015-09-06")
  group_by(date) %>%
  summarise(sentiment=sum(sentiment),price=sum(price))

For price, you could use max(), min() etc depending on what you want.

Lisa Clark
  • 330
  • 1
  • 2
  • 11