1
> dd <- data.frame(id=c("a","b","b","c","c","c"),names=1:6)
> target <- data.frame(id=c("a","b","c"),names = c("1","2,3","4,5,6"),counts=c(1,2,3))
> dd
  id names
1  a     1
2  b     2
3  b     3
4  c     4
5  c     5
6  c     6
> target
  id names counts
1  a     1      1
2  b   2,3      2
3  c 4,5,6      3

How may I convert dd to target; the best solution may be using pivotal_wider; but I cannot figure out how to do it.

Z. Zhang
  • 637
  • 4
  • 16

1 Answers1

1

Just group by id and summarise, i.e.

library(dplyr)

dd %>% 
 group_by(id) %>% 
 summarise(names = toString(names), 
           counts = n())
Sotos
  • 51,121
  • 6
  • 32
  • 66