根据R中的多个范围联接表 [英] Join tables based on multiple ranges in R
本文介绍了根据R中的多个范围联接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个要合并的数据框的情况。表格
params
以时间和角度范围描述了单位的参数。表 data
更长,并且包含id,时间和angle参数。
I have a situation where I have two data frames I would like to join. The table
params
describes the param for a unit in terms time and angle ranges. The table data
is longer and contains id, time and angle parameters.
我想加入参数值当id匹配且时间在有效数据源from_valid和有效数据源to之间,且ang在数据
中的angle_begin angle_end之间时,从 params
I would like to join the param value from params
when id match and time is in the range between valid_from and valid_to and ang is between angle_begin angle_end in the data
table.
以下是表的示例。
params <- data.frame(id = 1:4
,valid_from = 1
,valid_to = c(10, 20, 30, 40)
,angle_begin = c(120, 90, 0, 50)
,angle_end = c(180, 170, 160, 150)
,param = c("A", "B", "C", "D"))
data <- data.frame(id = rep(1:4, each=100)
,time = rep(seq(from = 0.5, to = 50, by = 0.5), 4)
,ang = rep(runif(100, 0, 360), 4))
推荐答案
使用 tidyverse
,您可以尝试一些ng like:
with tidyverse
, you can try something like:
data %>%
inner_join(params) %>%
filter( time > valid_from & time < valid_to) %>%
filter( ang > angle_begin & ang < angle_end)
这篇关于根据R中的多个范围联接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文