Skip to contents

Subset rows of a data frame. A combination of dplyr::slice() and dplyr::filter()

Usage

filter(.data, ..., .frame = NULL, .env = parent.frame())

# S3 method for class 'data.frame'
filter(.data, ..., .frame = NULL, .env = parent.frame())

# S3 method for class 'group_df'
filter(.data, ..., .frame = NULL, .env = parent.frame())

# S3 method for class 'nest_df'
filter(.data, ..., .frame = NULL, .env = parent.frame())

filter_(data, x, frame = NULL, env = parent.frame())

# S3 method for class 'data.frame'
filter_(data, x, frame = NULL, env = parent.frame())

# S3 method for class 'group_df'
filter_(data, x, frame = NULL, env = parent.frame())

# S3 method for class 'nest_df'
filter_(data, x, frame = NULL, env = parent.frame())

Arguments

.data, data

A data frame.

...

Expressions to be evaluated. Needs to result in a logical vector or integer vector representing the rows to keep.

.frame, frame

A character vector of column names referred to in x. This subsets the data frame to only specified columns before evaluation. This provides safer evaluation by preventing accidental use of spurious global variables in place of column names or accidental use of spurious column names in place of global variables.

.env, env

An environment to look for objects outside those in data.

x
Character vector

when evaluated, needs to result in a logical vector or integer vector representing the rows to keep. See details for more information.

Integer vector

for row positions to keep or remove.

Logical vector

of length nrow(data) for row positions to keep or remove.

Value

data.frame

Details

The evaluation of x is applied to each group in a nest_df or group_df or to the entire data frame in a data.frame. There are three allowed outcomes when evaluating x:

  1. A logical vector with length nrow(data).

  2. An integer vector of row positions to keep.

  3. A logical vector of length 1 indicating to keep (TRUE) or remove (FALSE) the entire data frame for a data.frame or individual group for a nest_df or group_df. For a data.frame, filter() will return a data.frame with zero rows if x evaluates to FALSE.

x can also be an integer vector or logical vector of length nrow(data) for traditional-like subsetting as data[x, , drop = FALSE].

Examples

#----------------------------------------------------------------------------
# filter() examples
#----------------------------------------------------------------------------
library(bkdat)

df <- data.frame(a = c(1, 2, 3), b = c(4, 5, 6))

# These expressions will be evaluated to a logical vector of row positions
# to keep.
filter_(df, "a > 2 | b < 5")
#>   a b
#> 1 1 4
#> 3 3 6
filter_(df, "a == 3")
#>   a b
#> 3 3 6

filter(df, a > 2 | b < 5)
#>   a b
#> 1 1 4
#> 3 3 6
filter(df, a == 3)
#>   a b
#> 3 3 6

# These expressions will be evaluated to a numeric vector of row positions
# to keep.
filter_(df, "which(a == 3)")
#>   a b
#> 3 3 6
filter_(df, "a")
#>   a b
#> 1 1 4
#> 2 2 5
#> 3 3 6
filter_(df, "1:2")
#>   a b
#> 1 1 4
#> 2 2 5
filter_(df, "1")
#>   a b
#> 1 1 4
filter_(df, "c(1, 3)")
#>   a b
#> 1 1 4
#> 3 3 6
filter_(df, "sample(a, length(a), replace = TRUE)")
#>     a b
#> 1   1 4
#> 3   3 6
#> 3.1 3 6

filter(df, which(a == 3))
#>   a b
#> 3 3 6
filter(df, a)
#>   a b
#> 1 1 4
#> 2 2 5
#> 3 3 6
filter(df, 1:2)
#>   a b
#> 1 1 4
#> 2 2 5
filter(df, 1)
#>   a b
#> 1 1 4
filter(df, c(1, 3))
#>   a b
#> 1 1 4
#> 3 3 6
filter(df, sample(a, length(a), replace = TRUE))
#>   a b
#> 1 1 4
#> 2 2 5
#> 3 3 6

# No evaluation
filter_(df, 1)
#>   a b
#> 1 1 4
filter_(df, -1)
#>   a b
#> 2 2 5
#> 3 3 6
filter_(df, c(TRUE, FALSE, FALSE))
#>   a b
#> 1 1 4

filter(df, 1)
#>   a b
#> 1 1 4
filter(df, -1)
#>   a b
#> 2 2 5
#> 3 3 6
filter(df, c(TRUE, FALSE, FALSE))
#>   a b
#> 1 1 4

# For group_df
df <- group_by(mtcars, c("cyl", "carb"))
filter_(df, "wt < 3.2 | mpg > 20")
#>                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4      21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> Merc 240D      24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#> Merc 230       22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
#> Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
#> Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
#> Toyota Corona  21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
#> Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
#> Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#> Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
#> Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
#> Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
#> Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

df <- group_by(mtcars, c("cyl", "carb"))
filter(df, wt < 3.2 | mpg > 20)
#>                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4      21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> Merc 240D      24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#> Merc 230       22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
#> Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
#> Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
#> Toyota Corona  21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
#> Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
#> Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#> Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
#> Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
#> Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
#> Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

# For nest_df
df <- nest_by(mtcars, c("cyl", "carb"))
filter_(df, "wt < 3.2 | mpg > 20")
#>   cyl carb
#> 1   6    4
#> 2   4    1
#> 3   6    1
#> 4   8    2
#> 5   8    4
#> 6   4    2
#> 7   8    3
#> 8   6    6
#> 9   8    8
#>                                                                                                                                                                                                                                                                                                                                                                                                                     data
#> 1                                                                                                                                                                                                                                                                               21.000, 21.000, 160.000, 160.000, 110.000, 110.000, 3.900, 3.900, 2.620, 2.875, 16.460, 17.020, 0.000, 0.000, 1.000, 1.000, 4.000, 4.000
#> 2                                                                        22.800, 32.400, 33.900, 21.500, 27.300, 108.000, 78.700, 71.100, 120.100, 79.000, 93.000, 66.000, 65.000, 97.000, 66.000, 3.850, 4.080, 4.220, 3.700, 4.080, 2.320, 2.200, 1.835, 2.465, 1.935, 18.610, 19.470, 19.900, 20.010, 18.900, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 0.000, 1.000, 4.000, 4.000, 4.000, 3.000, 4.000
#> 3                                                                                                                                                                                                                                                                                                                                                    21.400, 258.000, 110.000, 3.080, 3.215, 19.440, 1.000, 0.000, 3.000
#> 4                                                                                                                                                                                                                                                                                                                                                                                                                       
#> 5                                                                                                                                                                                                                                                                                                                                                             15.80, 351.00, 264.00, 4.22, 3.17, 14.50, 0.00, 1.00, 5.00
#> 6 24.400, 22.800, 30.400, 26.000, 30.400, 21.400, 146.700, 140.800, 75.700, 120.300, 95.100, 121.000, 62.000, 95.000, 52.000, 91.000, 113.000, 109.000, 3.690, 3.920, 4.930, 4.430, 3.770, 4.110, 3.190, 3.150, 1.615, 2.140, 1.513, 2.780, 20.000, 22.900, 18.520, 16.700, 16.900, 18.600, 1.000, 1.000, 1.000, 0.000, 1.000, 1.000, 0.000, 0.000, 1.000, 1.000, 1.000, 1.000, 4.000, 4.000, 4.000, 5.000, 5.000, 4.000
#> 7                                                                                                                                                                                                                                                                                                                                                                                                                       
#> 8                                                                                                                                                                                                                                                                                                                                                             19.70, 145.00, 175.00, 3.62, 2.77, 15.50, 0.00, 1.00, 5.00
#> 9                                                                                                                                                                                                                                                                                                                                                                                                                       

df <- nest_by(mtcars, c("cyl", "carb"))
filter(df, wt < 3.2 | mpg > 20)
#>   cyl carb
#> 1   6    4
#> 2   4    1
#> 3   6    1
#> 4   8    2
#> 5   8    4
#> 6   4    2
#> 7   8    3
#> 8   6    6
#> 9   8    8
#>                                                                                                                                                                                                                                                                                                                                                                                                                     data
#> 1                                                                                                                                                                                                                                                                               21.000, 21.000, 160.000, 160.000, 110.000, 110.000, 3.900, 3.900, 2.620, 2.875, 16.460, 17.020, 0.000, 0.000, 1.000, 1.000, 4.000, 4.000
#> 2                                                                        22.800, 32.400, 33.900, 21.500, 27.300, 108.000, 78.700, 71.100, 120.100, 79.000, 93.000, 66.000, 65.000, 97.000, 66.000, 3.850, 4.080, 4.220, 3.700, 4.080, 2.320, 2.200, 1.835, 2.465, 1.935, 18.610, 19.470, 19.900, 20.010, 18.900, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 0.000, 1.000, 4.000, 4.000, 4.000, 3.000, 4.000
#> 3                                                                                                                                                                                                                                                                                                                                                    21.400, 258.000, 110.000, 3.080, 3.215, 19.440, 1.000, 0.000, 3.000
#> 4                                                                                                                                                                                                                                                                                                                                                                                                                       
#> 5                                                                                                                                                                                                                                                                                                                                                             15.80, 351.00, 264.00, 4.22, 3.17, 14.50, 0.00, 1.00, 5.00
#> 6 24.400, 22.800, 30.400, 26.000, 30.400, 21.400, 146.700, 140.800, 75.700, 120.300, 95.100, 121.000, 62.000, 95.000, 52.000, 91.000, 113.000, 109.000, 3.690, 3.920, 4.930, 4.430, 3.770, 4.110, 3.190, 3.150, 1.615, 2.140, 1.513, 2.780, 20.000, 22.900, 18.520, 16.700, 16.900, 18.600, 1.000, 1.000, 1.000, 0.000, 1.000, 1.000, 0.000, 0.000, 1.000, 1.000, 1.000, 1.000, 4.000, 4.000, 4.000, 5.000, 5.000, 4.000
#> 7                                                                                                                                                                                                                                                                                                                                                                                                                       
#> 8                                                                                                                                                                                                                                                                                                                                                             19.70, 145.00, 175.00, 3.62, 2.77, 15.50, 0.00, 1.00, 5.00
#> 9