Home > Software design >  many value in one cell divided by space duplicate in R
many value in one cell divided by space duplicate in R

Time:10-02

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.

CodePudding user response:

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))
  •  Tags:  
  • r
  • Related