gpt4 book ai didi

r - 通过来自不同数据框的多个条件过滤数据框

转载 作者:行者123 更新时间:2023-12-01 00:37:51 26 4
gpt4 key购买 nike

我想根据不同数据帧( 'data' )中的几个值过滤一个数据帧( 'key' )。

我的 'key'看起来像这样

exhibit.name  <- c("lions", "otters", "penguins")
exhibit.start <- c(as.Date("2016-04-01"), as.Date("2016-05-01"), as.Date("2016-06-01"))
exhibit.end <- c(as.Date("2016-04-30"), as.Date("2016-05-31"), as.Date("2016-06-30"))
key <- data_frame(exhibit.name, exhibit.start, exhibit.end)

还有我的 'data'看起来像这样
exhibit.name <- c("lions", "lions", "otters", 
"otters", "penguins", "penguins")
exhibit.date <- c(as.Date("2016-04-15"), as.Date("2016-12-15"), as.Date("2016-05-15"),
as.Date("2016-02-15"), as.Date("2016-06-15"), as.Date("2016-10-15"))
data <- data_frame(exhibit.name, exhibit.date)

我需要过滤 'data'返回行,其中 data$exhibit.name匹配 key$exhibit.name和其 data$exhibit.date属于相关 key$exhibit.startkey$exhibit.end日期。生成的数据框如下所示:
> valid.exhibits
1|lions |2016-04-15
2|otters |2016-05-15
3|penguins|2016-06-15

谢谢!

最佳答案

我们可以做一个 left_join然后 filter

data %>% 
left_join(., key) %>%
filter(exhibit.start < exhibit.date, exhibit.end > exhibit.date) %>%
select(1:2)
# exhibit.name exhibit.date
# <chr> <date>
#1 lions 2016-04-15
#2 otters 2016-05-15
#3 penguins 2016-06-15

我们还可以使用 non-equi(来自 data.table 的开发版本的条件连接),即 v1.9.7+
library(data.table)
setDT(key)
setDT(data)[key, on = .(exhibit.name, exhibit.date > exhibit.start,
exhibit.date < exhibit.end), new := 1]
na.omit(data)[, new := NULL][]
# exhibit.name exhibit.date
#1: lions 2016-04-15
#2: otters 2016-05-15
#3: penguins 2016-06-15

关于r - 通过来自不同数据框的多个条件过滤数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39212698/

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