Cut numeric vector into intervals, but only returning the lower boundary for each element as numeric vector
Below is my attempt. It works, but I am looking for a less hacky and more general solution. I prefer a solution relying more on math than on functions.
library(tidyverse)
x = 1943:2023
y = cut(x, seq(1943, 2023, 5), include.lowest = TRUE, right = FALSE) |> as.character() |> str_sub(2, 5) |> as.numeric()
tibble(x, y) |> print(n=15)
#> # A tibble: 81 x 2
#> x y
#> <int> <dbl>
#> 1 1943 1943
#> 2 1944 1943
#> 3 1945 1943
#> 4 1946 1943
#> 5 1947 1943
#> 6 1948 1948
#> 7 1949 1948
#> 8 1950 1948
#> 9 1951 1948
#> 10 1952 1948
#> 11 1953 1953
#> 12 1954 1953
#> 13 1955 1953
#> 14 1956 1953
#> 15 1957 1953
#> # ... with 66 more rows
Any help appreciated!