Hello I'm new to R and I dont understand why my following approach does not work. I have this df1 that looks somethig like this:
view duration_hours date
1 a 5 2021-03-29
2 a 7 2021-03-29
3 a 3 2021-03-30
4 b 2 2021-03-30
5 b 5 2021-03-30
6 c 9 2021-03-30
7 c 2 2021-03-31
8 c 3 2021-04-01
I want a new data frame (df2) that sums the durations - over all views and split to the single views for a certain date
date duration duration_sum a b c
1 2021-03-29 12 12 0 0
2 2021-03-30 19 3 7 9
3 2021-03-31 2 0 0 2
4 2021-04-01 3 0 0 3
First, I tried the following just for the "overall" duration, worked as intented creating the "duration_sum" variable with the summed durations for every date
df2 <- df1 %>%
group_by(date) %>%
summarise(duration_sum = sum(duration_hours, na.rm = TRUE)
Then I tried to add the other variables by augmenting the code in the following way
df2<- df1 %>%
group_by(date) %>%
summarise(duration_sum = sum(duration_hours, na.rm = TRUE),
a =sum(duration_hours[view=="a"], na.r = TRUE),
b =sum(duration_hours[view=="b"], na.r = TRUE),
c =sum(duration_hours[view=="c"], na.r = TRUE))
But that did not yield the account to the right amounts. What do I do wrong?