I have a dataframe similar to the one generated by the following structure:
library(dplyr)
df1 <- expand.grid(region = c(\"USA\", \"EUR\", \"World\"),
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)))])