Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: slice_ends() #7109

Open
DesiQuintans opened this issue Dec 9, 2024 · 1 comment
Open

Suggestion: slice_ends() #7109

DesiQuintans opened this issue Dec 9, 2024 · 1 comment

Comments

@DesiQuintans
Copy link

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

@eutwt
Copy link
Contributor

eutwt commented Dec 25, 2024

You can also use row_number() in slice()

x %>% 
  arrange(age) %>% 
  group_by(age_group) %>% 
  slice(head(row_number(), 2), tail(row_number(), 2))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants