I have the folllowing dataset:
# A tibble: 90 × 2
decade n
<dbl> <int>
1 1930 13
2 1931 48
3 1932 44
4 1933 76
5 1934 73
6 1935 63
7 1936 54
8 1937 51
9 1938 41
10 1939 42
# … with 80 more rows
With more years that continue until 2010. I wish to "group" by decade like 1930s, 1940s, etc... and to have on another column the count of n in each year until the end of the decade.
For example:
# A tibble: 90 × 2
decade n
<dbl> <int>
1 1930-1939 449
2 1940-1949 516
Thanks!
CodePudding user response:
We could use the modulo operator %%
:
library(dplyr)
df %>%
group_by(decade = decade - decade %% 10) %>%
summarise(n = sum(n))
decade n
<dbl> <int>
1 1930 505