Home > Enterprise >  R data.table calculate sum of other rows
R data.table calculate sum of other rows

Time:07-07

Using R and data.table I've got this data

test = data.frame(genotypes =  c('A|A', 'A|G', 'G|G'), high = c(73, 113, 87), low = c(77, 155, 63))
genotypes high low
1       A|A   73  77
2       A|G  113 155
3       G|G   87  63

How can I get 3 new matrix of this data, containing 1 row and the sum of other rows by columns?

# low           |   high
# -----------------------------
# A|A           |   A|A
# -----------------------------
# A|G   G|G     |   A|G   G|G

So I need to get nrow(test) adiitional matrix in next view:

matrix(c(73, 113 87, 77, 155 63), nrow = 2)
matrix(c(113, 73 87, 155, 77 63), nrow = 2)
matrix(c(87, 73 113, 63, 77 155), nrow = 2)

example

> matrix(c(73, 113 87, 77, 155 63), nrow = 2)
     [,1] [,2]
[1,]   73   77
[2,]  200  218

How can do that?

CodePudding user response:

cols = c('high', 'low')
lapply(
  seq_len(nrow(df)), 
  \(i) matrix(c(unlist(df[i, cols]), colSums(df[-i, cols])), nrow = 2, byrow=TRUE)
)

[[1]]
     [,1] [,2]
[1,]   73   77
[2,]  200  218

[[2]]
     [,1] [,2]
[1,]  113  155
[2,]  160  140

[[3]]
     [,1] [,2]
[1,]   87   63
[2,]  186  232

Data

df = data.frame(genotypes =  c('A|A', 'A|G', 'G|G'), high = c(73, 113, 87), low = c(77, 155, 63))

CodePudding user response:

I would try

setDT(t)
lapply(t$genotypes, function(x){
    as.matrix(t[, lapply(.SD, sum), by = .(genotypes == x)][, !'genotypes'])
})
# [[1]]
#      high low
# [1,]   73  77
# [2,]  200 218
# 
# [[2]]
#      high low
# [1,]  160 140
# [2,]  113 155
# 
# [[3]]
#      high low
# [1,]  186 232
# [2,]   87  63
  • Related