I am trying to put the years 1928 to 2022 into decades categories in R, as I want to calculate the average return of the stock market for each decade
SP500 %>% mutate(decade = cut(SP500$Year, seq(1930,2020,by=10))) %>%
group_by(decade) %>% summarise(return = mean(`Annual\n% Change`))
# A tibble: 10 × 2
decade return
<fct> <dbl>
1 (1.93e+03,1.94e+03] 0.014
2 (1.94e+03,1.95e+03] 0.077
3 (1.95e+03,1.96e+03] 0.124
4 (1.96e+03,1.97e+03] 0.056
5 (1.97e+03,1.98e+03] 0.058
6 (1.98e+03,1.99e+03] 0.098
7 (1.99e+03,2e+03] 0.157
8 (2e+03,2.01e+03] 0.018
9 (2.01e+03,2.02e+03] 0.121
10 NA 0.04
how can i change the labels for the decade factor to something like 1930-1940, 1940-1950....
many thanks in advance
The
labels=
argument to the function you're using accepts a vector oflength(breaks)-1
for you to override the labels.Or perhaps to be more accurate with the range nomenclature, we can do