简体   繁体   中英

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 ?

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)

You can make a summarization:

data1 <- data[,.(type1 = sum(type1),
                 type2 = sum(type2),
                 type3 = sum(type3)
                 ), by = id]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM