How to select rows in data.frame without NA values

I have a data frame called data. I want to create a function f (data, collist). This function takes data and a list of columns from the data itself and returns only those rows from the data for which the mentioned column names in collist are not NA. I know that this can be done using for loop, but I want to do this without using for loop.

Also, please let me know if in R it is generally more efficient to avoid loops.

Here is an example:

ABCD 1 2 NA NA 2 NA NA NA NA 3 7 5 NA 4 2 NA 5 6 NA NA 

If the collist contains B and C, then a reduced data frame with row number 1,3,4 will be returned. The reason is either B or C, or both have NA in lines 2 and 5. I need a function because I will use this operation quite a few times. On this subject, I will learn some new R tricks, as well as make my program more elegant. Thanks.

+6
source share
1 answer

Looks like you're just looking for complete.cases . Here is an example:

 #### SAMPLE DATA set.seed(1) m <- matrix(rnorm(20), 5) m[sample(length(m), 7)] <- NA mydf <- data.frame(m) mydf # X1 X2 X3 X4 # 1 NA -0.8204684 1.511781 -0.04493361 # 2 0.1836433 0.4874291 NA NA # 3 -0.8356286 0.7383247 NA 0.94383621 # 4 1.5952808 NA -2.214700 0.82122120 # 5 0.3295078 NA NA 0.59390132 #### SAMPLE EXTRACTION collist <- c("X1", "X2") mydf[complete.cases(mydf[collist]), collist] # X1 X2 # 2 0.1836433 0.4874291 # 3 -0.8356286 0.7383247 
+8
source

Source: https://habr.com/ru/post/957698/


All Articles