Skip to contents

Subset Data Frames by a Group Variable Using Their Positions

Usage

group_slice(
  data,
  group,
  n = 1,
  prop = NULL,
  position = "head",
  group_output = FALSE
)

Arguments

data

A data frame or tibble with at least 1 variable.

group

A variable in data that will be used for groupings.

n, prop

Supply either n, the number of groups, or prop, the proportion of groups to select. n must be a positive integer that is greater than or equal to 1. prop must be a positive numeric value that is greater than 0 and less than or equal to 1.

Default is n = 1.

position

A character string of "head" or "tail". Determines if the first group or last group in the data frame is selected where "head" will select the first group in the dataframe and "tail" will select the last group.

group_output

A logical boolean TRUE or FALSE. If TRUE, returns a grouped tibble.

Default is FALSE

Value

A sliced dataframe

Examples

vec_coords <- 1:10
df_data <-
 data.frame(
   "x" = vec_coords,
   "y" = vec_coords,
   "group_col" = group_numbers(1:5) |> rep(each = 2)
 )

df_sliced_data_head <-
 df_data |>
 group_slice(group_col, n = 2, position = "head")

df_sliced_data_head
#>   x y group_col
#> 1 1 1         1
#> 2 2 2         1
#> 3 3 3         2
#> 4 4 4         2

df_sliced_data_tail <-
 df_data |>
 group_slice(group_col, n = 2, position = "tail")

df_sliced_data_tail
#>    x  y group_col
#> 4 10 10         5
#> 3  9  9         5
#> 2  8  8         4
#> 1  7  7         4

df_sliced_data_prop <-
 df_data |>
 group_slice(group_col, prop = .80)

df_sliced_data_prop
#>   x y group_col
#> 1 1 1         1
#> 2 2 2         1
#> 3 3 3         2
#> 4 4 4         2
#> 5 5 5         3
#> 6 6 6         3
#> 7 7 7         4
#> 8 8 8         4