Useful things R

Published

Thu, 12 of September, 2024

Modified

Fri, 14 of June, 2024

Filter rows in and out with dplyr

Multiple values (same column) in and out

  • %in% c("x", "y") for values
  • !colname for negation
library(dplyr)

starwars <- starwars

f_1 <- starwars %>% 
    filter(homeworld %in% c("Naboo","Tatooine"))  

f_2 <- starwars %>% 
    # ! in front! 
    filter(!homeworld %in% c("Naboo","Tatooine"))

Multiple columns (some values) in and out

f_3 <- starwars %>% 
    filter(species == "Ewok" & homeworld %in% c("Endor","Tatooine"))  

f_4 <- starwars %>% 
    # ! in front! 
    filter(!species %in% c("Droid","Human") &  !homeworld %in% c("Naboo","Tatooine"))

Keep/Drop rows that have ALL NA values in all columns

# Filter rows that have only NA values in all columns  
cols_allNA <- starwars %>%
  filter(if_all(everything(), is.na))  # 0 

# or drop them 
# Drop rows that have NA values in all columns
cols_noneNA <- starwars %>%
  filter(!if_all(everything(), is.na))  # 87 

Drop rows that have ANY NA values in all columns

# Filter rows that have only NA values in all columns  
cols_anyNA <- starwars %>%
  filter(!if_any(everything(), is.na))  # 58 

Keep/drop rows that have ALL NA values in some columns

# Filter rows that have only NA values in all columns whose name ....
spec_col_allNA <- starwars %>%
 filter(if_all(starts_with("h"), is.na))  # 0 
 # filter(!if_all(starts_with("h"), is.na))  # 0