通过数据压缩扩展数据帧

时间:2016-12-28 17:22:16

标签: r

与此类似的数据集......

    ID <- c(rep(10,4),rep(20,4),rep(30,4),rep(40,4),rep(50,4))
    Activity <- rep(c("In","Start","Finish","Out"),5)
    Rsn <- c(rep("Rsn1",4),rep("Rsn11",4),rep("Rsn111",4),rep("Rsn11",4),rep("Rsn111",4))
    Inst <- seq(1,20,1)
    Loc <- c(rep("Here",4),rep("There",4),rep("Anywhere",4),rep("Somewhere",4),rep("SomewhereElse",4))
    dc <- data.frame(ID,Activity,Rsn,Inst,Loc)

ID  Activity  Rsn     Inst  Loc 
10  In        Rsn1    1     Here
10  Start     Rsn1    2     Here
10  Finish    Rsn1    3     Here
10  Out       Rsn1    4     Here
20  In        Rsn11   5     There
20  Start     Rsn11   6     There
20  Finish    Rsn11   7     There
20  Out       Rsn11   8     There
30  In        Rsn111  9     Anywhere
30  Start     Rsn111  10    Anywhere
30  Finish    Rsn111  11    Anywhere
30  Out       Rsn111  12    Anywhere
40  In        Rsn11   13    Somewhere
40  Start     Rsn11   14    Somewhere
40  Finish    Rsn11   15    Somewhere
40  Out       Rsn11   16    Somewhere
50  In        Rsn111  17    SomewhereElse
50  Start     Rsn111  18    SomewhereElse
50  Finish    Rsn111  19    SomewhereElse
50  Out       Rsn111  20    SomewhereElse

我想最终得到的最终结果是......

ID2 <- c(10,20,30,40,50)
In2 <- seq(1,20,4)
Start2 <- seq(2,20,4)
Finish2 <- seq(3,20,4)
Out2 <- seq(4,20,4)
Rsn2 <- c("Rsn1","Rsn11","Rsn111","Rsn11","Rsn111")
Loc2 <- c("Here","There","Anywhere","Somewhere","SomewhereElse")
dw <- data.frame(ID2, In2, Start2, Finish2, Out2, Rsn2,Loc2)

ID  In2  Start2 Finish2 Out2  Rsn2    Loc2  
10  1    2      3       4     Rsn1    Here
20  5    6      7       8     Rsn11   There
30  9    10     11      12    Rsn111  Anywhere
40  13   14     15      16    Rsn11   Somewhere
50  17   18     19      20    Rsn111  SomewhereElse

我使用了tidyr,重塑并且似乎无法正确使用。

数据集有816K行,并且需要循环和条件。

我已经按位置打破了数据集,但它仍然很慢。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我们可以使用input

dcast