gpt4 book ai didi

仅检索 data.frame 中子组的前 %

转载 作者:行者123 更新时间:2023-12-02 05:23:14 26 4
gpt4 key购买 nike

我需要在 data.frame 中检索子组的前 %。关键是子组的长度不同,所以我不能选择任意等级来调整组子集的大小。我已经有一个排序正确的排名列。

vartype varname rank
a one 1
a two 2
b one 1
b two 2
b three 3
b four 4
c one 1
c two 2
c three 3

为上表中的每个 vartype 选择 rank 的前 50% 将返回:

vartype varname rank
a one 1
b one 1
b two 2
c one 1
c two 2

使用 floor 或 ceiling 作为截断并不重要。谢谢!!

最佳答案

如果您不介意使用 dplyr:

dat <- read.table(text = "vartype varname rank
a one 1
a two 2
b one 1
b two 2
b three 3
b four 4
c one 1
c two 2
c three 3",header = TRUE,sep = "")

> dat %>% group_by(vartype) %>% filter(percent_rank(rank) <= 0.5)
Source: local data frame [5 x 3]
Groups: vartype

vartype varname rank
1 a one 1
2 b one 1
3 b two 2
4 c one 1
5 c two 2

关于仅检索 data.frame 中子组的前 %,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23921392/

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