Open
Description
I often want to know what discretising a continuous variable actually looks like in my data, so I often want to know what happens at the edges of level. Maybe this sort of thing is useful to enough other people that it's worth making a dedicated slice_
function?
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
set.seed(12345)
x <-
data.frame(
age = runif(50, min = 0, max = 100)
) %>%
mutate(
age_group =
cut(
age,
breaks = c(0, 18, 26, 32, 50, 60, 75, 100),
include.lowest = TRUE
)
)
x %>%
arrange(age) %>%
group_by(age_group) %>%
{
bind_rows(
slice_head(., n = 2),
slice_tail(., n = 2)
) %>%
arrange(age)
}
#> # A tibble: 24 × 2
#> # Groups: age_group [6]
#> age age_group
#> <dbl> <fct>
#> 1 0.114 [0,18]
#> 2 0.599 [0,18]
#> 3 16.6 [0,18]
#> 4 17.9 [0,18]
#> 5 18.8 (18,26]
#> 6 22.6 (18,26]
#> 7 22.6 (18,26]
#> 8 26.0 (18,26]
#> 9 32.1 (32,50]
#> 10 32.5 (32,50]
#> # ℹ 14 more rows
Created on 2024-12-09 with reprex v2.1.1
Metadata
Assignees
Labels
No labels