Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select rows from a data frame where any variable is not NA [duplicate]

Tags:

r

Possible Duplicate:
Removing empty rows of a data file in R

Suppose I have a dataframe df

I would like to select the rows from it, where any of the variables in the row are not NA. That is to say I only want to exclude the rows in which all the variables are NA

like image 544
Joe King Avatar asked Jul 05 '12 10:07

Joe King


People also ask

How do you select rows that are not na?

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 you omit Na in a data frame?

To remove all rows having NA, we can use na. omit function. For Example, if we have a data frame called df that contains some NA values then we can remove all rows that contains at least one NA by using the command na. omit(df).

How do I select only certain rows in R?

By using bracket notation on R DataFrame (data.name) we can select rows by column value, by index, by name, by condition e.t.c. You can also use the R base function subset() to get the same results. Besides these, R also provides another function dplyr::filter() to get the rows from the DataFrame.


1 Answers

df[apply(!is.na(df), 1, any), ]
like image 171
Thierry Avatar answered Sep 28 '22 08:09

Thierry