How to subset a data frame by taking only the Non NA values of 2 columns in this data frame

EnginO picture EnginO · Feb 13, 2015 · Viewed 89.4k times · Source

I am trying to subset a data frame by taking the integer values of 2 columns om my data frame

Subs1<-subset(DATA,DATA[,2][!is.na(DATA[,2])] & DATA[,3][!is.na(DATA[,3])])

but it gives me an error : longer object length is not a multiple of shorter object length.

How can I construct a subset which is composed of NON NA values of column 2 AND column 3?

Thanks a lot?

Answer

cogitovita picture cogitovita · Feb 13, 2015

Try this:

Subs1<-subset(DATA, (!is.na(DATA[,2])) & (!is.na(DATA[,3])))

The second parameter of subset is a logical vector with same length of nrow(DATA), indicating whether to keep the corresponding row.