When should I use “which” for subsetting?

后端 未结 3 668
猫巷女王i
猫巷女王i 2020-12-19 14:10

It is a toy example.

 iris %>% 
  group_by(Species) %>% 
  summarise(max = Sepal.Width[Sepal.Length == max(Sepal.Length)])

 # A tibble: 3 x 2
  Specie         


        
3条回答
  •  自闭症患者
    2020-12-19 14:29

    The which removes the NA elements. If we need to get the same behavior as which where there are NAsuse another condition along with==`

    iris %>% 
      group_by(Species) %>% 
      summarise(max = Sepal.Width[Sepal.Length == max(Sepal.Length, na.rm = TRUE) & 
                                       !is.na(Sepal.Length)])
    

提交回复
热议问题