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

many value in one cell divided by space duplicate in R

name high_test
Rober 45 78 66 89
Kevin 33 51 51 67
Adelaide 71 87 60 98
Alexis 28 28 29 28
df <- data.frame(
  name = c("Rober", "Kevin", "Adelaide", "Alexis"),
  high_test = c("45 78 66 89","33 51 51 67","71 87 60 98","28 28 29 28"))

> str(df)
'data.frame':   4 obs. of  2 variables:
 $ name     : chr  "Rober" "Kevin" "Adelaide" "Alexis"
 $ high_test: chr  "45 78 66 89" "33 51 51 67" "71 87 60 98" "28 28 29 28"

value in column high_test is divided by space,
I want de- duplicate the same value.

name high_test
Rober 45 78 66 89
Kevin 33 51 67
Adelaide 71 87 60 98
Alexis 28 29

Could someone tell me how can I do? Thank you.

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 :

You can split the string on the space, look for unique elements, and paste back together:

df$high_test <- lapply(strsplit(df$high_test," "), \(s) paste(unique(s),collapse=" "))

Output:

      name   high_test
1    Rober 45 78 66 89
2    Kevin    33 51 67
3 Adelaide 71 87 60 98
4   Alexis       28 29

An alternative that does the same thing is below:

f <- function(s) paste(unique(s),collapse=" ")

dplyr::mutate(df, high_test = lapply(strsplit(high_test," "), f))
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