Remove rows with all or some NAs (missing values) in data.frame

后端 未结 16 1585
日久生厌
日久生厌 2020-11-21 05:49

I\'d like to remove the lines in this data frame that:

a) contain NAs across all columns. Below is my example data frame.



        
16条回答
  •  清酒与你
    2020-11-21 06:14

    One approach that's both general and yields fairly-readable code is to use the filter() function and the across() helper functions from the {dplyr} package.

    library(dplyr)
    
    vars_to_check <- c("rnor", "cfam")
    
    # Filter a specific list of columns to keep only non-missing entries
    
    df %>% 
      filter(across(one_of(vars_to_check),
                    ~ !is.na(.x)))
    
    # Filter all the columns to exclude NA
    df %>% 
      filter(across(everything(),
                    ~ !is.na(.)))
    
    # Filter only numeric columns
    df %>%
      filter(across(where(is.numeric),
                    ~ !is.na(.)))
    

    Similarly, there are also the variant functions in the dplyr package (filter_all, filter_at, filter_if) which accomplish the same thing:

    library(dplyr)
    
    vars_to_check <- c("rnor", "cfam")
    
    # Filter a specific list of columns to keep only non-missing entries
    df %>% 
      filter_at(.vars = vars(one_of(vars_to_check)),
                ~ !is.na(.))
    
    # Filter all the columns to exclude NA
    df %>% 
      filter_all(~ !is.na(.))
    
    # Filter only numeric columns
    df %>%
      filter_if(is.numeric,
                ~ !is.na(.))
    

提交回复
热议问题