Subset Data Frames by a Group Variable Using Their Positions
Arguments
- data
A data frame or tibble with at least 1 variable.
- group
A variable in
datathat will be used for groupings.- n, prop
Supply either
n, the number of groups, orprop, the proportion of groups to select.nmust be a positive integer that is greater than or equal to 1.propmust 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
TRUEorFALSE. IfTRUE, returns a grouped tibble.Default is
FALSE
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
