使用data.table,将变量连接成单个变量

时间:2014-05-02 23:32:53

标签: r data.table

说我有以下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"))

创建:

      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

我想要的是一个新变量c,用于连接每个变量中的任何内容,所以它看起来有点像:

      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"

我认为我在正确的轨道上用

之类的东西
dt[, .SD[,b], by=a]

返回

      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

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:3)

你想要这样的东西吗?

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