Convert list of years into another series of numbers R

Solution 1:

Shouldn't it be

ifelse(select_data$Yr_Sqnce <6, select_data$Yr_Sqnce, "6+")

Solution 2:

I'm unsure what data looks like, so I just created a minimal example here. You don't need to get the max ahead of time; you can just use max in a mutate statement to calculate the new numbers. Then, I used an ifelse inside of mutate to replace the 6+ values.

library(tidyverse)

df %>% 
  mutate(group = (max(year) - year)+1), 
         group = ifelse(group >= 6, "6+", group))

Output

  year group
1 2021     1
2 2020     2
3 2019     3
4 2018     4
5 2017     5
6 2016    6+
7 2015    6+
8 2014    6+

Data

df <-
  structure(list(year = c(
    2021, 2020, 2019, 2018, 2017, 2016, 2015, 2014
  )),
  class = "data.frame",
  row.names = c(NA,-8L))