Search code examples
rdata.tablereshapedcast

Reshaping in R using data.table from multiple rows


While there are many posts on Stackoverlow about reshaping data in R, I cannot seem to find one explaining how to deal with my situation.

I have a dataset shaped like this, where each row represents includes a binary if the id is related to type 1,2 or 3.

data <- data.table( id    = c(1,1,1,2,2,2,3,3,3),
                    type1 = c(1,0,0,0,0,1,0,0,0), 
                    type2 = c(0,1,0,0,1,0,1,0,0), 
                    type3 = c(0,0,1,0,0,0,0,1,0))

> data
       id type1 type2 type3
    1:  1     1     0     0
    2:  1     0     1     0
    3:  1     0     0     1
    4:  2     0     0     0
    5:  2     0     1     0
    6:  2     1     0     0
    7:  3     0     1     0
    8:  3     0     0     1
    9:  3     0     0     0

However, I would like this information to be contained into one row per id value.

> data
   id type1 type2 type3
1:  1     1     1     1
2:  2     1     1     0
3:  3     0     1     1

How can I solve this using data.table?


Solution

  • library(data.table)
    data <- data.table( id    = c(1,1,1,2,2,2,3,3,3),
                        type1 = c(1,0,0,0,0,1,0,0,0), 
                        type2 = c(0,1,0,0,1,0,1,0,0), 
                        type3 = c(0,0,1,0,0,0,0,1,0))
    
    
    vars <- grep("^type", names(data), value = T)
    data[, lapply(.SD, sum), .SDcols = vars, by = id]
    #>    id type1 type2 type3
    #> 1:  1     1     1     1
    #> 2:  2     1     1     0
    #> 3:  3     0     1     1
    

    Created on 2021-02-11 by the reprex package (v1.0.0)