Apply a filter on range of variable if condition is met This function is a simple wrapper for filter

conditional_range_filter(df1, cond, varName, varCond)

Arguments

df1

A dataframe

cond

A logical value indicating if filter should be applied

varName

Name of variable to filter on

varCond

A vector with lower and upper bound on variable. Use NA values to perform a one sided filter.

See also

Examples

data(starwars, package = "dplyr") starwars %>% conditional_range_filter(TRUE, "height", c(160, 180))
#> # A tibble: 30 x 14 #> name height mass hair_color skin_color eye_color birth_year sex gender #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> #> 1 Luke… 172 77 blond fair blue 19 male mascu… #> 2 C-3PO 167 75 NA gold yellow 112 none mascu… #> 3 Owen… 178 120 brown, gr… light blue 52 male mascu… #> 4 Beru… 165 75 brown light blue 47 fema… femin… #> 5 Wilh… 180 NA auburn, g… fair blue 64 male mascu… #> 6 Han … 180 80 brown fair brown 29 male mascu… #> 7 Gree… 173 74 NA green black 44 male mascu… #> 8 Jabb… 175 1358 NA green-tan… orange 600 herm… mascu… #> 9 Wedg… 170 77 brown fair hazel 21 male mascu… #> 10 Jek … 180 110 brown fair blue NA male mascu… #> # … with 20 more rows, and 5 more variables: homeworld <chr>, species <chr>, #> # films <list>, vehicles <list>, starships <list>
starwars %>% conditional_range_filter(TRUE, "height", c(180, NA))
#> # A tibble: 43 x 14 #> name height mass hair_color skin_color eye_color birth_year sex gender #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> #> 1 Dart… 202 136 none white yellow 41.9 male mascu… #> 2 Bigg… 183 84 black light brown 24 male mascu… #> 3 Obi-… 182 77 auburn, w… fair blue-gray 57 male mascu… #> 4 Anak… 188 84 blond fair blue 41.9 male mascu… #> 5 Wilh… 180 NA auburn, g… fair blue 64 male mascu… #> 6 Chew… 228 112 brown unknown blue 200 male mascu… #> 7 Han … 180 80 brown fair brown 29 male mascu… #> 8 Jek … 180 110 brown fair blue NA male mascu… #> 9 Boba… 183 78.2 black fair brown 31.5 male mascu… #> 10 IG-88 200 140 none metal red 15 none mascu… #> # … with 33 more rows, and 5 more variables: homeworld <chr>, species <chr>, #> # films <list>, vehicles <list>, starships <list>