A reshape puzzle in data.table

后端 未结 3 519
遥遥无期
遥遥无期 2021-01-19 13:07

Yet another reshape problem in data.table

set.seed(1234)
DT <- data.table(x=rep(c(1,2,3),each=4), y=c(\"A\",\"B\"), v=sample(1:100,12))
#             


        
3条回答
  •  旧时难觅i
    2021-01-19 13:58

    Try this:

    cumsum0 <- function(x) { x <- cumsum(x); ifelse(x == 0, NA, x) }
    DT2 <- DT[, {SUM.<-y; lapply(data.table(model.matrix(~ SUM.:x + SUM.:v + 0)), cumsum0)}]
    setnames(DT2, sub("(.):(.)", "\\2.\\1", names(DT2)))
    

    Simplifications:

    1) If using 0 in place of NA is ok then it can be simplified by omitting the first line which defines cumsum0 and replacing cumsum0 in the next line with cumsum.

    2) The result of the second line has these names:

    > names(DT2)
    [1] "SUM.A:x" "SUM.B:x" "SUM.A:v" "SUM.B:v"
    

    so if that is sufficient the last line can be dropped since its only purpose is to make the names exactly the same as in the question.

    The result (without the simplifications) is:

    > DT2
        SUM.x.A SUM.x.B SUM.v.A SUM.v.B
     1:       1      NA      12      NA
     2:       1       1      12      62
     3:       2       1      72      62
     4:       2       2      72     123
     5:       4       2     155     123
     6:       4       4     155     220
     7:       6       4     156     220
     8:       6       6     156     242
     9:       9       6     255     242
    10:       9       9     255     289
    11:      12       9     318     289
    12:      12      12     318     338
    

提交回复
热议问题