Considering the following example vector (class is numeric):
dates <- data.frame(A = c(2021, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2023,
2023, 2023, 2023, 2024, 2025, 2025, 2025, 2025), B = "something")
I want to remove any of the daes that are repeated:
dates <- data.frame(A = c(2021, 2022, NA, NA, NA, NA, NA, NA, NA, 2023,
NA, NA, NA, 2024, 2025, NA, NA, NA), B = "something")
>Solution :
Use duplicated
dates$A <- replace(dates$A, duplicated(dates$A), NA)
Or update the original object
dates$A[duplicated(dates$A)] <- NA
if these are based on adjacent values, use rle
dates$A <- replace(dates$A, duplicated(with(rle(dates$A),
rep(seq_along(values), lengths))), NA)
If we want to remove the rows
subset(dates, !duplicated(A))