Rilcon42
Rilcon42

Reputation: 9765

compressing vector into comma seperated string

How can I compress the results of unique into a comma delimited string?

temp3 <- data.frame(categoryId=c(1,1,1,2),ballot1=c("yes","yes","no","200"))
temp3 %>% group_by(categoryId)
      %>% mutate(responses=unique(ballot1))

Expected Output:

1 yes,no
1 yes,no
1 yes,no
2 200

Upvotes: 0

Views: 52

Answers (1)

akrun
akrun

Reputation: 886948

We can group by 'categoryId', get the unique elements in 'ballot1' and paste them together

library(dplyr) 
temp3 %>% 
   group_by(categoryId) %>%
   mutate(ballot1 = toString(unique(ballot1)))
# A tibble: 4 x 2
# Groups:   categoryId [2]
#   categoryId ballot1
#       <dbl>   <chr>
#1          1 yes, no
#2          1 yes, no
#3          1 yes, no
#4          2     200

Upvotes: 1

Related Questions