Charles Baillie
Charles Baillie

Reputation: 53

Identify and replace all sum-to-zero rows in a list of binary matrices

I'm looking for a solution to the following problem - there must be a simple answer!

I have a list of binary matrices,and I would like to identify all the rows for every matrix in which every element on that row is zero (= row sums to zero) and then replace each element on that row with 1/ncol(DF). Every matrix has the same number of rows but a different number of columns. My final data set will have over 5,000 matrices!

So something like this:

     a     b     c
abc  0     1     0
def  0     0     1
ghi  0     0     0 
jkl  1     0     0

to become:

     a    b    c
abc  0    1    0
def  0    0    1
ghi  0.33 0.33 0.33
jkl  1    0    0

Thanks!

Upvotes: 1

Views: 92

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73385

For a single matrix A, you can do

A[!rowSums(A), ] <- 1 / ncol(A)

Example

A <- structure(c(0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0), .Dim = c(4L, 4L))

#     [,1] [,2] [,3] [,4]
#[1,]    0    0    0    1
#[2,]    0    1    1    0
#[3,]    1    0    0    1
#[4,]    0    0    0    0

The code above gives:

#     [,1] [,2] [,3] [,4]
#[1,] 0.00 0.00 0.00 1.00
#[2,] 0.00 1.00 1.00 0.00
#[3,] 1.00 0.00 0.00 1.00
#[4,] 0.25 0.25 0.25 0.25

When you have a list of matrices, say lst, you can lapply through the list:

lapply(lst, function (A) {A[!rowSums(A), ] <- 1 / ncol(A); return(A)})

Upvotes: 2

Related Questions