TarJae
TarJae

Reputation: 78927

dplyr: How to slice row1 of group1, row2 of group2, row3 of group3, ...rowN of groupN

This question is inspired by this question: How to use conditional filtering of a data frame in R when trying to retain non-duplicated values in two columns

How can we slice in subsequent groups (1,2,3,4...n) the corresponding rows (1,2,3,4....n). In this simplified example the first dataframe should result in the second:

library(tidyverse)
# tibble
df <- tibble(
  group = c(1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5),
  value = c(1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5)
)

enter image description here should result in this enter image description here

So far I have tried different options with slice like slice(seq(1, n(), by = 2)) from here: How to get every nth element from each group in a grouped data frame. Many thanks!

Upvotes: 1

Views: 84

Answers (2)

Wimpel
Wimpel

Reputation: 27732

and a data.table solution to be complete ;-)

library( data.table )
setDT(df)[, .SD[.GRP], by = group]

explanation from the help file:
.SD is a data.table containing the Subset of x’s [wimpel: here x == df] Data for each group, excluding any columns used in by (or keyby).
.GRP is an integer, length 1, containing a simple group counter. 1 for the 1st group, 2 for the 2nd, etc.

Upvotes: 2

Gregor Thomas
Gregor Thomas

Reputation: 145775

df %>%
  group_by(group) %>%
  slice(cur_group_id())
# # A tibble: 5 x 2
# # Groups:   group [5]
#   group value
#   <dbl> <dbl>
# 1     1     1
# 2     2     2
# 3     3     3
# 4     4     4
# 5     5     5

Upvotes: 7

Related Questions