0

I have looked already on the forum and there are some similar questions, but each time slightly different. Therefore I decided to post a new one. I just can't figure this out.

Consider I have these data

   id score
1  1     3
2  1     6
3  2     2
4  2     4
5  2     3
6  3     9
7  3     2

And I want it to look like this:

  id     score
1  1     3, 6
2  2     2, 3, 4
3  3     2, 9

Suggestions are very welcome! Thank you in advance.

neilfws
  • 32,751
  • 5
  • 50
  • 63
3353206
  • 52
  • 6

1 Answers1

1

aggregate() can do that:

df <- read.table(header=TRUE, text=
'   id score
1  1     3
2  1     6
3  2     2
4  2     4
5  2     3
6  3     9
7  3     2')

aggregate(score ~ id, data=df, FUN=paste0)
# > aggregate(score ~ id, data=df, FUN=paste0)
#   id   score
# 1  1    3, 6
# 2  2 2, 4, 3
# 3  3    9, 2
jogo
  • 12,469
  • 11
  • 37
  • 42