Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove repeated elements from a column in r

Tags:

r

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")
like image 786
Maral Dorri Avatar asked Nov 29 '25 12:11

Maral Dorri


2 Answers

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
like image 139
TarJae Avatar answered Dec 01 '25 02:12

TarJae


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))
like image 22
akrun Avatar answered Dec 01 '25 04:12

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!