是否有R函数可以为嵌套数据模型修改数据框?

时间:2019-04-01 15:32:02

标签: r dataframe

我正在尝试为嵌套数据模型修改数据框,但无法弄清楚如何使用R。

这是我的数据框:

idquestionario cod_cartao ord_cartao
             1      33333          5
             1      Death          4
             1      12123          2
             1      22332          3
             1      32332          5
             1      11111          1
            10      23222          7
            10      Death          4
            10      12233          2
            10      33333          4
            10      12111          1
            10      22332          3
...

我需要重塑数据框以获得以下结果:

idquestionario cod_cartao cod_cartao
             1      11111      12123
             1      11111      22332
             1      11111      22332
             1      11111      Death
             1      11111      32332
             1      11111      33333
             1      12123      22332
             1      12123      Death
             1      12123      32332
             1      12123      33333
             1      22332      22332
             1      22332      Death
             1      22332      32332
             1      22332      33333
             1      Death      32332
             1      Death      33333
            10      12111      12233
            10      12111      22332
            10      12111      22332
            10      12111      33333
            10      12233      22332
            10      12233      33333
            10      22332      33333
...

请尝试解释一下我的数据框。我有三列(idquestionario,cod_cartao,ord_cartao)

  • idquestionario是一个索引数字列
  • cod_cartao是用于衡量生活质量的卡代码
  • ord_cartao是纸牌的排名(1是最好的,2是第二最好的,依此类推)。您可以在此排名中保持联系。

例如id = 1,我们的排名为:1、2、3、4和5。排名与卡片直接相关:

1-11111
2-12123
3-22332
4-死亡
5-33333和32332

所以我需要使用排名代码来匹配所有卡:

1 2
1 3
1 4
1 5 Doesn't matter the order
1 5 Doesn't matter the order
2 3
2 4
2 5 Doesn't matter the order
2 5 Doesn't matter the order
3 4
3 5 Doesn't matter the order
3 5 Doesn't matter the order
4 5 Doesn't matter the order
4 5 Doesn't matter the order

感谢您的耐心和帮助我!

1 个答案:

答案 0 :(得分:0)

您可以这样做:

library(data.table)
setDT(DT)

DT[order(ord_cartao), 
  CJ(cod1 = cod_cartao, cod2 = cod_cartao, sorted = FALSE)[ CJ(V1 = ord_cartao, V2 = ord_cartao, sorted = FALSE)[, V1 < V2] ]
, by=idquestionario]

    idquestionario  cod1  cod2
 1:              1 11111 12123
 2:              1 11111 22332
 3:              1 11111 Death
 4:              1 11111 33333
 5:              1 11111 32332
 6:              1 12123 22332
 7:              1 12123 Death
 8:              1 12123 33333
 9:              1 12123 32332
10:              1 22332 Death
11:              1 22332 33333
12:              1 22332 32332
13:              1 Death 33333
14:              1 Death 32332
15:             10 12111 12233
16:             10 12111 22332
17:             10 12111 Death
18:             10 12111 33333
19:             10 12111 23222
20:             10 12233 22332
21:             10 12233 Death
22:             10 12233 33333
23:             10 12233 23222
24:             10 22332 Death
25:             10 22332 33333
26:             10 22332 23222
27:             10 Death 23222
28:             10 33333 23222
    idquestionario  cod1  cod2

工作原理。第一个CJ采用代码矢量的笛卡尔组合。第二个对顺序向量执行相同的操作。谓词V1 < V2是为顺序向量计算的,并用于过滤为代码向量生成的表。

注释。这似乎是一种非常低效的数据整理方式,因为表大小会膨胀,而不会传达任何其他信息。而且,它将丢弃所有代码具有相同等级的id。如果要存储有关等级的信息,则可以将igraph包与相邻等级创建的链接一起使用。我的意思是这样的图,如此处顶部附近的图所示:https://en.wikipedia.org/wiki/Graded_poset