I have such a data frame with annual data, however, for some years there is no data (here: 1956, 1961-1964).
dat <- data.frame(Year = c(1950:1955, 1957:1960, 1965:1970),
Val = 1:16)
> dat
Year Val
1 1950 1
2 1951 2
3 1952 3
4 1953 4
5 1954 5
6 1955 6
7 1957 7
8 1958 8
9 1959 9
10 1960 10
11 1965 11
12 1966 12
13 1967 13
14 1968 14
15 1969 15
16 1970 16
I'd like to add a variable "Period" with the min and max years for each period, where a period is defined as a set of continuous years (i.e. 1950-1955, 1957-1960 and 1965-1970). Creating this variable is not a problem itself, but I am stuck on how to do the grouping. Any ideas?
dat %>%
...???... %>%
mutate(Period = paste(min(Year), max(Year), sep = "-"))
See Question&Answers more detail:os