mardi 28 janvier 2020

Dplyr, filtering groups if any value in vector list exists

I want to filter out by a grouping variable if any of a vector of values in a variable exist in the group, i.e. there may be no groups where any variable is (Value V) V1, V2 or V3.

E.g. There may remain no groups wherein a tree which is susceptible to disease strain/ injury type V1, V2 or V3 is present.

However, my calls keep being interpreted: There may remain no trees in a group which is susceptible to disease strain/injury type V1, V2 or V3.

Example 1:

df %>% group_by(tree_group) %>% 
  filter(any(!(tree_condition %in% c("Significant","Severe","Dead or dying")))) %>%
  filter(any(!(injury_type_1 %in% c("Significant","Severe","Dead or dying")))) %>%
  filter(any(!(injury_type_2 %in% c("Significant","Severe","Dead or dying")))) %>%
  filter(any(!(injury_type_3 %in% c("Significant","Severe","Dead or dying"))))

Example 2:

df  %>% group_by(tree_group) %>% 
  filter(!(any(tree_condition %in% c("Significant","Severe","Dead or dying")))) %>%
  filter(!(any(injury_type_1 %in% c("Significant","Severe","Dead or dying")))) %>%
  filter(!(any(injury_type_2 %in% c("Significant","Severe","Dead or dying")))) %>%
  filter(!(any(injury_type_3 %in% c("Significant","Severe","Dead or dying"))))

Both example 1 and example 2 yield the same result - damaged trees being removed from the groups, not incident groups being removed from the call.

I also tried, without succeeding, to create a variable (DAMAGED) to mark all trees in the group with 1, for susceptible if one member was susceptible, and else 0:

df %>% 
group_by(tree_group) %>% mutate(if (tree_condition %in% c("Significant","Severe","Dead or dying")){
    DAMAGED=1
  } else if(injury_type_1 %in% c("Significant","Severe","Dead or dying")) {
    DAMAGED=1
  } else if(injury_type_2 %in% c("Significant","Severe","Dead or dying")) {
    DAMAGED=1
  } else if(injury_type_3 %in% c("Significant","Severe","Dead or dying")) {
    DAMAGED=1
  } else {
    DAMAGED=0
  })

However, this throws an condition as:

1: In if (tree_condition %in% c("Significant", "Severe", "Dead or dying")) { :
  the condition has length > 1 and only the first element will be used
2: In if (injury_type_1 %in% c("Significant", "Severe", "Dead or dying")) { :
  the condition has length > 1 and only the first element will be used
3: In if (injury_type_2 %in% c("Significant", "Severe", "Dead or dying")) { :
  the condition has length > 1 and only the first element will be used
4: In if (injury_type_3 %in% c("Significant", "Severe", "Dead or dying")) { :
  the condition has length > 1 and only the first element will be used

Aucun commentaire:

Enregistrer un commentaire