1

I have a huge matrix with multiple columns and rows. It contains numeric values and NAs.

Here is my problem: I have some rows only consisting of NAs meaning they are basically empty. These rows interfere with my further calculations but I also can not just delete them.

Now to my question:

Is there a quick function I can use on my matrix to replace the NAs in these rows with the same single value (lets say 0 or 99, this is irrelevant!)? But -important- ONLY for the rows which are empty (only NAs)?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
brunktier
  • 75
  • 6

3 Answers3

3

You just need to index the rows which are all NA, and then replace them with an assignment. You can achieve this with is.na(), rowSums(), and ncol(). I've written them all out below, so you can see the working:

x <- rbind(c(1, 2, NA), c(NA, NA, NA), c(NA, 5, 6), c(7, NA, 9))
x
is.na(x)
rowSums(is.na(x))
ncol(x)
# This is the single line that does all the work
x[rowSums(is.na(x)) == ncol(x),] <- 99
x

Output:

     [,1] [,2] [,3]
[1,]    1    2   NA
[2,]   NA   NA   NA
[3,]   NA    5    6
[4,]    7   NA    9
      [,1]  [,2]  [,3]
[1,] FALSE FALSE  TRUE
[2,]  TRUE  TRUE  TRUE
[3,]  TRUE FALSE FALSE
[4,] FALSE  TRUE FALSE
[1] 1 3 1 1
[1] 3
     [,1] [,2] [,3]
[1,]    1    2   NA
[2,]   99   99   99
[3,]   NA    5    6
[4,]    7   NA    9
ajrwhite
  • 7,728
  • 1
  • 11
  • 24
1
mdat
          C.1 C.2 C.3
row1        1   2   3
row2       11  12  13
rowpartNA   2   2  NA
rowNA      NA  NA  NA

all.na.fun<- function(x)all(is.na(x))
apply(mdat, 1, all.na.fun)
     row1      row2 rowpartNA     rowNA 
    FALSE     FALSE     FALSE      TRUE 
> all.na.fun<- function(x)all(is.na(x))
(all.na.row<-apply(mdat, 1, all.na.fun))
     row1      row2 rowpartNA     rowNA 
    FALSE     FALSE     FALSE      TRUE 
mdat[all.na.row,] <-99
mdat
          C.1 C.2 C.3
row1        1   2   3
row2       11  12  13
rowpartNA   2   2  NA
rowNA      99  99  99
Stephen Henderson
  • 6,340
  • 3
  • 27
  • 33
1

The matrix:

m <- matrix(c(1,2,3,4,
             NA,NA,NA,NA,
             NA, 1, 2, 3,
             1,2, NA, 4, 
             4,2,1,3),  nrow=5, byrow = T )

The syntax:

m[which(rowSums(is.na(m))==ncol(m)),]=999

The output:

> m
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]  999  999  999  999
[3,]   NA    1    2    3
[4,]    1    2   NA    4
[5,]    4    2    1    3
Diego Rojas
  • 199
  • 6