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

Combined slice_head and slice_tails into one function #7078

Closed
james-kilgour opened this issue Aug 28, 2024 · 2 comments
Closed

Combined slice_head and slice_tails into one function #7078

james-kilgour opened this issue Aug 28, 2024 · 2 comments

Comments

@james-kilgour
Copy link

There's currently no quick way to extract both the first and last rows of a grouped data frame. This is useful if, for example, you want to identify the limits from a range of values in an ordered sequence.

Currently, the workflow looks something like:

### Option one:

df_1 <- df %>%
        group_by(v1, v2) %>%
        arrange(v1, v2) %>%
        slice_head()

df_2 <- df %>%
        group_by(v1, v2) %>%
        arrange(v1, v2) %>%
        slice_tail()

data <- rbind(data_1, data_2)

### Option two:

df <- df %>%
        group_by(v1, v2) %>%
        arrange(v1, v2) %>%
        filter(row_number()==1 | row_number()==n()) 

The following code combines existing dplyr functionality to make this operation more intuitive:

slice_ends <- function(data){

    data_1 <- data %>%
        slice_head()

    data_2 <- data %>%
        slice_tail()

    rbind(data_1, data_2)

}

# To give:

df <- df %>%
        group_by(v1, v2) %>%
        arrange(v1, v2) %>%
        slice_ends()
@ks8997
Copy link

ks8997 commented Aug 31, 2024

Not sure if this is necessary. I like:

iris %>% 
  group_by(Species) %>% 
  slice(1, n())

@etiennebacher
Copy link

Just as a complement to @ks8997's nice answer, here's how to customize the number of head and tail rows:

library(dplyr, warn.conflicts = FALSE)

iris %>% 
  # Get the first 2 rows and the last 3 rows by group
  slice(1:2, (n() - 2):n(), .by = Species)
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 1           5.1         3.5          1.4         0.2     setosa
#> 2           4.9         3.0          1.4         0.2     setosa
#> 3           4.6         3.2          1.4         0.2     setosa
#> 4           5.3         3.7          1.5         0.2     setosa
#> 5           5.0         3.3          1.4         0.2     setosa
#> 6           7.0         3.2          4.7         1.4 versicolor
#> 7           6.4         3.2          4.5         1.5 versicolor
#> 8           6.2         2.9          4.3         1.3 versicolor
#> 9           5.1         2.5          3.0         1.1 versicolor
#> 10          5.7         2.8          4.1         1.3 versicolor
#> 11          6.3         3.3          6.0         2.5  virginica
#> 12          5.8         2.7          5.1         1.9  virginica
#> 13          6.5         3.0          5.2         2.0  virginica
#> 14          6.2         3.4          5.4         2.3  virginica
#> 15          5.9         3.0          5.1         1.8  virginica

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

3 participants