How to insert a new row after a group of rows with the mean of the values grouped by date ?
id date price
1 2022-01-01 4
2 2022-01-01 2
3 2022-01-01 2
1 2022-01-02 5
2 2022-01-02 3
3 2022-01-02 1
Desired output
id date price
1 2022-01-01 4
2 2022-01-01 2
3 2022-01-01 2
mean 2022-01-01 2.66
1 2022-01-02 5
2 2022-01-02 3
3 2022-01-02 1
mean 2022-01-02 3
You can do this (although I can't really understand why you would want your data in this format):
bind_rows(
dat %>% mutate(id = as.character(id)),
dat %>% group_by(date) %>%
summarize(price=mean(price)) %>%
mutate(id = "mean")
) %>%
arrange(date,id)
Output:
id date price
<char> <IDat> <num>
1: 1 2022-01-01 4.000000
2: 2 2022-01-01 2.000000
3: 3 2022-01-01 2.000000
4: mean 2022-01-01 2.666667
5: 1 2022-01-02 5.000000
6: 2 2022-01-02 3.000000
7: 3 2022-01-02 1.000000
8: mean 2022-01-02 3.000000
Perhaps better just to do this:
dat %>% group_by(date) %>% mutate(mean = mean(price))
Output:
id date price mean
<int> <date> <int> <dbl>
1 1 2022-01-01 4 2.67
2 2 2022-01-01 2 2.67
3 3 2022-01-01 2 2.67
4 1 2022-01-02 5 3
5 2 2022-01-02 3 3
6 3 2022-01-02 1 3