2

I have a character matrix with differing numbers of NA in different rows. I would like to create a structure that has the same number of rows, with all the NAs removed.

Peter Flom
  • 2,008
  • 4
  • 22
  • 35

1 Answers1

7

Using a simple apply :

x <- matrix(1:10,ncol=2)
x[c(4,7,8)] <- NA

apply(x,1,na.exclude)

edit : if all rows have exactly the same amount of NA's, then this will return a matrix. For conversion of this matrix to a list, see : How to convert a matrix to a list of column-vectors in R? .

Community
  • 1
  • 1
Joris Meys
  • 106,551
  • 31
  • 221
  • 263