This function picks rows from a dataframe by matching n unique values from the head (or tail) of a column. It is similar to dplyr::top_n(), but instead of using min_rank() or max_rank(), it uses utils::head() or utils::tail(); and var is flexible as in dplyr::pull().

pick_top(.data, var, n = 1)

Arguments

.data

A table of data.

var

A variable specified as:

  • a literal variable name

  • a positive integer, giving the position counting from the left

  • a negative integer, giving the position counting from the right.

The default returns the last column (on the assumption that is the column you have created most recently). This argument is taken by expression and supports quasiquotation (you can unquote column names and column positions).

n

Number of values used for matching, from the head (or tail) of var.

Value

A filtered version of the input dataset.

See also

dplyr::pull, dplyr::top_n, utils::head(), utils::tail().

Other general functions to pick or drop rows of a dataframe: in_top, pick_recensus

Examples

df <- data.frame(x = 1:9, y = letters[1:3], stringsAsFactors = FALSE) # `var` can be bare or quoted (result <- pick_top(df, "y"))
#> x y #> 1 1 a #> 4 4 a #> 7 7 a
identical(pick_top(df, y), result)
#> [1] TRUE
# matching `var` by position starting from the left identical(pick_top(df, var = y), pick_top(df, var = 2))
#> [1] TRUE
# matching `var` by position starting from the right identical(pick_top(df, var = y), pick_top(df, var = -1))
#> [1] TRUE
pick_top(df, y, n = 2)
#> x y #> 1 1 a #> 2 2 b #> 4 4 a #> 5 5 b #> 7 7 a #> 8 8 b
# Negative values select from the tail pick_top(df, y, n = -2)
#> x y #> 2 2 b #> 3 3 c #> 5 5 b #> 6 6 c #> 8 8 b #> 9 9 c