Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace all columns in matrix with NA based on variable in another dataframe?

Tags:

r

I have a large adjacency matrix and a dataframe with a variable 'missing'. I need to replace all rows in the matrix with NA, for which 'missing' = TRUE (rows match between matrix and dataframe).

Example: I have: matrix

     [,1] [,2] [,3]
[1,]    0    0    1
[2,]    1    0    1
[3,]    0    1    0

and dataframe

  ID missing
1  1   FALSE
2  2   FALSE
3  3    TRUE

I need: matrix

     [,1] [,2] [,3]
[1,]    0    0    1
[2,]    1    0    1
[3,]   NA    NA    NA

Reproducible data:

m1.data <- c(0, 1, 0, 0, 0, 1, 1, 1, 0 )
m1 <- matrix(m1.data, nrow = 3)

df <- data.frame(ID = c(1, 2, 3),
                missing = c(FALSE, FALSE, TRUE))

Hope that someone can help! Thank you so much in advance!

like image 344
Stefanie Richters Avatar asked Sep 15 '25 19:09

Stefanie Richters


1 Answers

We can extract the missing column and use that as the row index in m1 and assign it to NA

m1[df$missing,] <- NA

-output

> m1
     [,1] [,2] [,3]
[1,]    0    0    1
[2,]    1    0    1
[3,]   NA   NA   NA

Or we may do

> NA^(df$missing) * m1
     [,1] [,2] [,3]
[1,]    0    0    1
[2,]    1    0    1
[3,]   NA   NA   NA
like image 107
akrun Avatar answered Sep 17 '25 08:09

akrun