Execute dplyr operation only if column exists

前端 未结 6 1824
青春惊慌失措
青春惊慌失措 2021-02-07 09:18

Drawing on the discussion on conditional dplyr evaluation I would like conditionally execute a step in pipeline depending on whether the reference column exists in the passed da

6条回答
  •  灰色年华
    2021-02-07 09:52

    With across() in dplyr > 1.0.0 you can now use any_of when filtering. Compare original with all columns:

    mtcars %>% 
      filter(am == 1) %>% 
      filter(cyl == 4)
    

    With cyl removed, it throws an error:

    mtcars %>% 
      select(!cyl) %>% 
      filter(am == 1) %>% 
      filter(cyl == 4)
    

    Using any_of (note you have to write "cyl" and not cyl):

    mtcars %>% 
      select(!cyl) %>% 
      filter(am == 1) %>% 
      filter(across(any_of("cyl"), ~.x == 4))
    #N.B. this is equivalent to just filtering by `am == 1`.
    

提交回复
热议问题