gpt4 book ai didi

r - 与 dplyr 等长的组

转载 作者:行者123 更新时间:2023-12-01 22:24:24 24 4
gpt4 key购买 nike

我有 df:

df <- data.frame(group = c(rep("G1",18), rep("G2", 10)), X = c(rep("a", 10), rep("b", 8), rep("c", 4), rep("d", 6)), Y = c(rep(1:10), rep(1:8), rep(1:4), rep(1:6)))

可能通过使用 dplyrtidyr,我想让每个 group 中的所有子组具有相同的长度,应该是组元素中最小的一个。简而言之,生成的数据框应该是:

df_r <- data.frame(group = c(rep("G1",16), rep("G2", 8)), X = c(rep("a", 8), rep("b", 8), rep("c", 4), rep("d", 4)), Y = c(rep(1:8), rep(1:8), rep(1:4), rep(1:4)))

我无法专注于如何实现这一目标。任何帮助将不胜感激。

最佳答案

这可能是您想要的?

library(dplyr)
df_r <- df %>% group_by(group, X) %>% mutate(maxY = max(Y)) %>%
group_by(group) %>% filter(Y <= min(maxY)) %>% select(group, X, Y)

> df_r
group X Y
1 G1 a 1
2 G1 a 2
3 G1 a 3
4 G1 a 4
5 G1 a 5
6 G1 a 6
7 G1 a 7
8 G1 a 8
9 G1 b 1
10 G1 b 2
11 G1 b 3
12 G1 b 4
13 G1 b 5
14 G1 b 6
15 G1 b 7
16 G1 b 8
17 G2 c 1
18 G2 c 2
19 G2 c 3
20 G2 c 4
21 G2 d 1
22 G2 d 2
23 G2 d 3
24 G2 d 4

> df_r1 <- data.frame(group = c(rep("G1",16), rep("G2", 8)), X = c(rep("a", 8), rep("b", 8), rep("c", 4), rep("d", 4)), Y = c(rep(1:8), rep(1:8), rep(1:4), rep(1:4)))
> identical(df_r, df_r1)
[1] TRUE

关于r - 与 dplyr 等长的组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37123421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com