【发布时间】:2021-05-31 08:38:03
【问题描述】:
这个问题的灵感来自这个问题:How to use conditional filtering of a data frame in R when trying to retain non-duplicated values in two columns
我们如何在后续组 (1,2,3,4...n) 中分割相应的行 (1,2,3,4....n)。 在这个简化的示例中,第一个数据帧应该导致第二个:
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)
)
到目前为止,我已经尝试了 slice 的不同选项,例如 slice(seq(1, n(), by = 2)) 从这里:How to get every nth element from each group in a grouped data frame。
非常感谢!
【问题讨论】: