Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

Removing rows of a matrix with at least one NA in R

I am trying to remove the rows of a matrix in R. My criteria for row removal is that the column contains at least one NA (note that the entire row does not need to be NA like in other questions on this site).

For example, if matrix A looks like the following

32  54  34
NA  10  NA
17  93  NA

Only the first row should remain.

This is what I am trying, but I am getting a column instead of a row.

newMatrix <- A[,rowSums(apply(A, 1, is.na))<1]
#A is a matrix

What am I missing? I tried doing !is.na, but R didn't like that.

question from:https://stackoverflow.com/questions/65939424/removing-rows-of-a-matrix-with-at-least-one-na-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Did you try: matrix[complete.cases(matrix),]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...