Reputation: 515
Say I have the following data.table:
dt <- data.table(a=c("AAA", "BBB", "CCC", "ABC", "CBA", "BAC", "CAB", "AAA", "BBB", "CCC", "ABC", "CBA", "BAC", "CAB"),
b=c("One", "Two", "Three", "Four", "Five", "Six"))
which creates:
a b
1: AAA One
2: BBB Two
3: CCC Three
4: ABC Four
5: CBA Five
6: BAC Six
7: CAB One
8: AAA Two
9: BBB Three
10: CCC Four
11: ABC Five
12: CBA Six
13: BAC One
14: CAB Two
What I want is a new variable, c, to concatenate whatever's in each of a, so it would look kindof like:
a c
1: AAA "One, Two"
2: BBB "Two, Three"
3: CCC "Three, Four"
4: ABC "Four, Five"
5: CBA "Five, Six"
6: BAC "Six, One"
7: CAB "One, Two"
I think I'm on the right track with something like
dt[, .SD[,b], by=a]
which returns
a V1
1: AAA One
2: AAA Two
3: BBB Two
4: BBB Three
5: CCC Three
6: CCC Four
7: ABC Four
8: ABC Five
9: CBA Five
10: CBA Six
11: BAC Six
12: BAC One
13: CAB One
14: CAB Two
Any ideas? Thank you!
Upvotes: 2
Views: 313
Reputation: 4615
Are you wanting something like this?
dt[,list(c=list(I(b))),by="a"]
a c
1: AAA One,Two
2: BBB Two,Three
3: CCC Three,Four
4: ABC Four,Five
5: CBA Five,Six
6: BAC Six,One
7: CAB One,Two
Upvotes: 3