Filter rows via integer/numeric position or logical vector

filter_data(.data, ...)

Arguments

.data

Data frame or two dimensional array

...

Each argument/expression should should evaluate and reduce down to an integer (row number) or logical vector. The filter will keep all row numbers that appear in all evaluated expressions (commas are the equivalent to &. Row numbers higher than what exists in x will be ignored. Any numeric vector must be either all positive or all negative (excludes). This function uses non-standard evaluation–users can refer to column names without quotations.

Value

Sliced/filtered data frame

Examples

set.seed(12) d <- data.frame( mpg = rnorm(100, 25, 3), gear = sample(3:6, 100, replace = TRUE), vs = sample(0:1, 100, replace = TRUE), stringsAsFactors = FALSE ) filter_data(d, mpg > 30)
#> mpg gear vs #> 1 31.02160 3 0 #> 2 31.21611 3 1 #> 3 31.06100 3 0 #> 4 30.86232 6 0
filter_data(d, !mpg < 30)
#> mpg gear vs #> 1 31.02160 3 0 #> 2 31.21611 3 1 #> 3 31.06100 3 0 #> 4 30.86232 6 0
filter_data(d, mpg > 30, !mpg < 30)
#> mpg gear vs #> 1 31.02160 3 0 #> 2 31.21611 3 1 #> 3 31.06100 3 0 #> 4 30.86232 6 0
filter_data(d, mpg > 30, gear == 4)
#> mpg gear vs #> 1 20.55830 6 1 #> 2 29.73151 3 1 #> 3 22.12977 5 1 #> 4 22.23998 6 1 #> 5 19.00707 6 0 #> 6 24.18311 5 1 #> 7 24.05395 4 0 #> 8 23.11523 3 1 #> 9 24.68061 5 1 #> 10 26.28404 4 1 #> 11 22.66684 6 1 #> 12 21.11835 3 1 #> 13 22.66130 4 1 #> 14 25.03586 5 0 #> 15 24.54275 5 0 #> 16 22.88961 5 0 #> 17 28.56664 3 1 #> 18 26.02154 4 1 #> 19 26.52090 6 1 #> 20 24.12008 3 1 #> 21 25.67092 5 0 #> 22 31.02160 3 0 #> 23 28.03594 4 1 #> 24 24.09262 5 0 #> 25 21.92427 4 0 #> 26 24.19785 3 0 #> 27 24.40268 6 1 #> 28 25.39337 5 1 #> 29 25.43740 3 0 #> 30 26.08619 5 0 #> 31 27.02194 3 0 #> 32 31.21611 3 1 #> 33 23.37691 4 0 #> 34 21.78852 4 1 #> 35 23.88263 4 1 #> 36 23.54458 4 0 #> 37 25.82435 5 0 #> 38 23.56146 4 1 #> 39 27.39432 6 1 #> 40 21.98665 6 1 #> 41 25.31495 4 0 #> 42 21.53202 3 0 #> 43 26.73440 6 0 #> 44 20.21312 4 0 #> 45 24.07449 5 0 #> 46 26.34840 5 0 #> 47 22.06884 3 0 #> 48 25.56999 6 1 #> 49 27.19436 3 1 #> 50 23.52220 5 1 #> 51 24.87195 6 1 #> 52 24.66199 3 0 #> 53 26.37048 4 0 #> 54 31.06100 3 0 #> 55 21.84733 6 1 #> 56 27.20396 6 0 #> 57 26.61775 3 1 #> 58 21.05718 6 0 #> 59 24.24988 4 0 #> 60 25.94261 3 0 #> 61 26.21964 6 0 #> 62 27.98326 4 0 #> 63 27.56731 5 0 #> 64 25.59139 4 0 #> 65 27.50298 3 0 #> 66 27.54037 3 1 #> 67 30.86232 6 0 #> 68 18.55222 6 0 #> 69 27.91336 4 0 #> 70 28.43518 6 1 #> 71 23.42380 6 0 #> 72 25.75096 3 0 #> 73 23.71178 5 0 #> 74 24.45244 5 1 #> 75 24.69007 6 1 #> 76 23.09849 5 0 #> 77 21.18684 3 1 #> 78 23.84815 5 1 #> 79 26.55027 5 1 #> 80 24.46609 3 1 #> 81 25.01277 6 1 #> 82 21.17782 3 0 #> 83 24.39367 3 0 #> 84 28.49340 4 0 #> 85 24.92986 6 1 #> 86 27.69147 3 1 #> 87 24.46983 6 1 #> 88 28.34113 3 0 #> 89 23.37433 3 1 #> 90 22.10981 6 1 #> 91 26.12935 3 1 #> 92 22.04598 6 0 #> 93 27.69268 3 1 #> 94 25.38779 6 0 #> 95 28.10111 5 1 #> 96 23.97313 4 0 #> 97 26.35684 3 0 #> 98 22.91579 5 0 #> 99 24.28296 3 0 #> 100 21.97810 6 1
filter_data(d, mpg > 30 | gear == 4, vs == 1)
#> mpg gear vs #> 1 26.28404 4 1 #> 2 22.66130 4 1 #> 3 26.02154 4 1 #> 4 28.03594 4 1 #> 5 31.21611 3 1 #> 6 21.78852 4 1 #> 7 23.88263 4 1 #> 8 23.56146 4 1