10

I want to replace my NA values from a matrix acquired by :

read.table(…)

Those values should be the mean of the corresponding row.

I.e, the following row of the table :

1 2 1 NA 2 1 1 2

would become

1 2 1 1.43 2 1 2

Thank you.

3
  • 2
    Why would you want to do this row-wise? Just checking you aren't mixing up variables with objects/samples. Usually one does this column-wise, computing the mean for each variable and using that to replace NA within the variable. Aug 2, 2011 at 21:21
  • Also, read.table() returns a data.frame. Are you talking about a data frame or a proper matrix? Aug 2, 2011 at 21:21
  • @GavinSimpson One reason for this would be in questionnaire data with repeated questions for use in a measurement. The means of the other questions would be used to substitute missing data.
    – Irwin
    Dec 12, 2013 at 4:52

3 Answers 3

30

Here's some sample data.

m <- matrix(1:16, nrow=4)
m[c(1,4,6,11,16)] <- NA

And here's how I'd fill in missings with the row means.

k <- which(is.na(m), arr.ind=TRUE)
m[k] <- rowMeans(m, na.rm=TRUE)[k[,1]]

Your data will be in a data.frame; you'll have to convert to a matrix first using as.matrix. You may or may not want to leave it in that format; to convert back use as.data.frame.

3
  • Thank you. However I get the following error message using your code : Error in [<-.data.frame(*tmp*, k, value = c(3.67857142857143, 3.34375, : only logical matrix subscripts are allowed in replacement
    – Delphine
    Aug 3, 2011 at 7:28
  • 1
    Make your data frame into a matrix first (as.matrix), then do it, then convert back (as.data.frame). Aug 3, 2011 at 17:37
  • 1
    As of the time of writing this comment, this solution works for data frames without conversion.
    – Irwin
    Dec 12, 2013 at 4:51
5
x[is.na(x)] <- mean(x, na.rm=TRUE)  # for vectors or for a matrix as a whole

t( apply(x, 1, function(xv) { xv[is.na(xv)] <- 
                                    mean(xv, na.rm=TRUE)
                              return(xv)}
          ) ) # for a row-oriented sol'n
2
  • 3
    Wouldn't this return the mean of the entire matrix? Aug 2, 2011 at 20:29
  • It would. I didn't get that he wanted a row oriented solution but will put one in.
    – IRTFM
    Aug 2, 2011 at 20:37
1
a = c(NA, 1, 2, 3, 10)
a[which(is.na(a)==TRUE)] = mean(a,na.rm = T)
9
  • 3
    This should work, but it's unnecessarily complicated. is.na(a) returns a vector of Booleans, so the == TRUE is redundant. which is not necessary either, since you can index vectors either by a vector of length <= length(a) or by a vector of length length(a) containing TRUEs and FALSEs (or 0/1's which get coerced to TRUE/FALSE). Finally, avoid using T and F for TRUE and FALSE, since they can get overwritten. Aug 2, 2011 at 20:32
  • I considered more, the training aspect :d
    – Areza
    Aug 2, 2011 at 20:37
  • For a matrix, same problem, takes the mean of everything and replaces. Aug 2, 2011 at 20:38
  • @BrandonBertelsen: Read the question again, and you're right. Aaron's got the solution using rowMeans. Aug 2, 2011 at 20:45
  • @user702846: Don't mean to discourage you though! Keep at it. Aug 2, 2011 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.