Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove repeated elements from a column in 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")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading