Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 387
EnginO Avatar asked Feb 13 '15 09:02

EnginO


People also ask

How do you subset data without NA in R?

To select rows of an R data frame that are non-Na, we can use complete. cases function with single square brackets. For example, if we have a data frame called that contains some missing values (NA) then the selection of rows that are non-NA can be done by using the command df[complete. cases(df),].

How do I subset a Dataframe in R by columns?

If you wanted to get the subset of a data. frame (DataFrame) Rows & Columns in R, either use the subset() function , filter() from dplyr package or R base square bracket notation df[] . subset() is a generic R function that is used to get the rows and columns (In R terms observations & variables) from the data frame.

How do you find non missing values in R?

To find the sum of non-missing values in an R data frame column, we can simply use sum function and set the na. rm to TRUE. For example, if we have a data frame called df that contains a column say x which has some missing values then the sum of the non-missing values can be found by using the command sum(df$x,na.


1 Answers

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.

like image 140
cogitovita Avatar answered Sep 19 '22 07:09

cogitovita