gpt4 book ai didi

r - 按特定列按另一个数据框过滤一个数据框

转载 作者:行者123 更新时间:2023-12-02 17:16:35 25 4
gpt4 key购买 nike

full = data.frame(group = c('a', 'a', 'a', 'a', 'a', 'b', 'c'), values = c(1, 2, 2, 3, 5, 3, 4))
filter = data.frame(group = c('a', 'b', 'c'), values = c(4, 3, 3))
## find rows of full where values are larger than filter for the given group
full[full$group == filter$group & full$values > filter$values, ]

打印一个带有警告的空 data.frame:

Warning messages: 1: In full$group == filter$group : longer object length is not a multiple of shorter object length 2: In full$values > filter$values : longer object length is not a multiple of shorter object length

我正在寻找 full 中符合该条件的所有行,最终得到:满

> group
group values
a 5
c 4

最佳答案

使用合并

full=merge(full,filter,by='group')
full=full[full$values.x>full$values.y,]
full$values.y=NULL
names(full)=c('group','values')
> full
group values
5 a 5
7 c 4

匹配

full$Filter=filter$values[match(full$group,filter$group)]
full=full[full$values>full$Filter,]
full$Filter=NULL
> full
group values
5 a 5
7 c 4

关于r - 按特定列按另一个数据框过滤一个数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46101658/

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