嵌套在 2 个数据帧之间循环

Nested for loop between 2 data frames

我想做以下过程: 我在一侧有一个名为 response_time_to_origin 的向量(参见 link : response_time_to_origin) and on another side a data frame called list_hit_windows (see the link : list_hit_windows),其中包含 3 列,即。 target_onset、target_onset_plus200 和 target_onset_plus1000。在此数据框的每一行,target_onset_plus200 和 target_onset_plus1000 应被视为间隔终端。

通过一些代码,我想知道对于response_time_to_origin的每一行,值是否包含在target_onset_plus200 和target_onset_plus1000 之间一个或多个list_hit_window 数据框的行(如果是,return TRUE,如果没有,return FALSE)。

我应该怎么做?

一般而言,我会将 list_hit_windows 添加为 response_time_to_origin 中的新列表列。然后,我将取消列表列的嵌套,以便为 response_time_to_origin 的每个值重复 list_hit_windows。然后你可以创建一个虚拟变量来判断每一行是否符合你的标准

df %>% mutate(meets_criteria = response_time_to_origin > target_onset_plus200 & response_time_to_origin < target_onset_plus1000)

这将根据要求为您提供 TRUE 和 FALSE 列。

你可以看看这个答案的最后一点R - calculate daily/weekly rate with changing denominator,它相似且可重现,供你学习。