nasim
nasim

Reputation: 735

Select rows with non-NA values in at least one of the desired columns

I have this code that works fine:

CompleteCoxObs<-temp[is.na(temp[,8])== FALSE | is.na(temp[,9])== FALSE | is.na(temp[,10])== FALSE,];

What is a better and more efficient way to achieve the same result?

Upvotes: 1

Views: 6117

Answers (3)

Johanna
Johanna

Reputation: 11

temp[apply(!is.na(temp[,8:10]),1,any),]

Note placing the "!" in front of is.na

Upvotes: 1

skan
skan

Reputation: 7720

You can try one of the followings:

temp[!is.na(rowSums(temp[,8:10])),]

or

temp[!apply(is.na(temp[,8:10]),1,any),]

or

temp[na.omit(temp[,8:10]),]

Upvotes: 2

M--
M--

Reputation: 28826

You can try this to check for all the columns:

CompleteCox.df <- temp.df[rowSums(is.na(temp.df)) != ncol(temp.df),]

In your case:

CompleteCox.df <- temp.df[rowSums(is.na(temp.df[, c(8,9,10)])) != 3,]

Upvotes: 4

Related Questions