R remove groups with only NAs

前端 未结 1 1859
死守一世寂寞
死守一世寂寞 2020-12-12 00:41

I have a dataframe similar to the one generated by the following structure:

library(dplyr)

df1 <- expand.grid(region   = c(\"USA\", \"EUR\", \"World\"),
         


        
相关标签:
1条回答
  • 2020-12-12 01:03

    To keep only combinations of region and variable that have at least 1 non-NA entry in value you can use:

    df %>% group_by(region, variable) %>% filter(any(!is.na(value)))
    

    Or equivalently:

    df %>% group_by(region, variable) %>% filter(!all(is.na(value)))
    

    And with data.table you could use:

    library(data.table)
    setDT(df)[, if(any(!is.na(value))) .SD, by = .(region, variable)]
    

    An approach in base R could be:

    df_split <- split(df, interaction(df$region, df$scenario, df$variable))
    do.call(rbind.data.frame, df_split[sapply(df_split, function(x) any(!is.na(x$value)))])
    
    0 讨论(0)
提交回复
热议问题