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")
CodePudding user response:
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))
CodePudding user response:
Here is how you can implement akrun's solution with mutate
:
library(dplyr)
dates %>%
mutate(A = replace(A, duplicated(A), NA))
A B
1 2021 something
2 2022 something
3 NA something
4 NA something
5 NA something
6 NA something
7 NA something
8 NA something
9 NA something
10 2023 something
11 NA something
12 NA something
13 NA something
14 2024 something
15 2025 something
16 NA something
17 NA something
18 NA something