0

I have some data like below:

A B
1 a
2 b
1 c
3 d
2 e

I want to transform this into something like: (A and B are column headers)

A B
1 c(a,c)
2 c(b,e)
3 d

I tried using aggregate but it is not turning out as expected.

Pierre Lapointe
  • 16,017
  • 2
  • 43
  • 56

1 Answers1

0

You could use tapply() with a function call to c(), like so:

A <- rep(letters[1:2], 5)
B <- rep(1:2, each = 5)

tapply(A, B, c)

You'll get a list as output:

$`1`
[1] "a" "b" "a" "b" "a"

$`2`
[1] "b" "a" "b" "a" "b"
Johan Larsson
  • 3,496
  • 18
  • 34