Say I have a dataframe like this:
test <- data.frame(x = c('v01','v02','v03'),
y = c('v01','v05','v06'),
z = c('v03', 'v03','v08'))
I can unite x:z columns by tidyverse pacakge:
test %>%
as.tbl %>%
unite(new_col, x:y:z, sep = ',', remove = F)
and here is the result:
new_col x y z
<chr> <fct> <fct> <fct>
1 v01,v01,v03 v01 v01 v03
2 v02,v05,v03 v02 v05 v03
3 v03,v06,v08 v03 v06 v08
but what I desire is unique values, like first row only has 'v01, v03':
new_col x y z
<chr> <fct> <fct> <fct>
1 v01,v03 v01 v01 v03
2 v02,v05,v03 v02 v05 v03
3 v03,v06,v08 v03 v06 v08
Any help?
Take your new column and split it by sep = ",". Then grab only unique elements ant paste it:
test <- data.frame(x = c('v01','v02','v03'),
y = c('v01','v05','v06'),
z = c('v03', 'v03','v08'))
test = test %>% unite(new_col, x:z, sep = ',', remove = F)
test$new_col = sapply(strsplit(test$new_col, ","),
function(x) new_col = paste(unique(x), collapse = ","))
Another way, you can do in one line without using unite
:
test$new_col <- apply(test, 1, function(x) paste(unique(x), collapse = ','))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With