具有基于 R 中两列的值范围的条件语句

Condition statement with range of values based off two columns in R

我正在尝试使用 True 或 False 填充我在数据框“DIVE”中创建的列,具体取决于数字是否位于数据框中两列的数字之间。 如果纪元号介于 dive_start 和 dive_end 之间,那么我希望 DIVE 等于 TRUE。如果它不在 dive_start 和 dive_end 之间,我希望 DIVE 等于 FALSE。 我已经根据类似的 Whosebug 帖子尝试过这个,但没有成功。所以,我想我只是问我的问题,看看我是否遗漏了什么。

数据如下(以 16hz 采样,因此具有重复的时间戳):

            datetime      epoch   diveNum dive_start   dive_end DIVE
1   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
2   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
3   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
4   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
5   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
6   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
7   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
8   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
9   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
10  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
11  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
12  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
13  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
14  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
15  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
16  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   NA
17  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA
18  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA
19  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA
20  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA
21  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA
22  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA
23  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA
24  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA
25  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA
26  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA
27  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA
28  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA
29  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA
30  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   NA

我尝试过的一些方法没有奏效,以下是其中一些方法:

#I got these suggestions from other Whosebugs, not sure if it won't work for what I'm trying to do. Or maybe I edited them to my data wrong.

df$DIVE <- ifelse(sapply(df$epoch, function(p){
  any(df$dive_end <= p & df$dive_start >= p),"TRUE", "FALSE"))
} 

#a second one I tried                 
for (i in 1:nrow(df)){
  if (df$epoch[i] >= df$dive_start & df$epoch[i] <= df$dive_end){
    df$DIVE[i] == "TRUE"
} else {
    df$DIVE[i] == "FALSE"
}
}
 
#a third option I tried that didn't work 
df %>%
  mutate(DIVE = map_chr(
    .x = epoch,
    .f = ~ if_else(
      condition = any(.x >= dive_start & .x <= dive_end),
      true = "TRUE",
      false = "FALSE"
    )
  ))

在我提供的示例数据中,如果代码有效,则 DIVE 列中的所有 NA 值都将变为 TRUE。但是,对于我列出的所有我尝试过的选项,它们都保留了值 NA 并且通常我不得不停止代码继续 运行 因为它不起作用并且只是保持 运行ning .我一定是遗漏了什么或者没有正确编写代码。

预期输出:

            datetime      epoch   diveNum dive_start   dive_end  DIVE
1   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
2   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
3   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
4   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
5   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
6   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
7   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
8   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
9   2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
10  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
11  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
12  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
13  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
14  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
15  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
16  2018-04-06 14:47:51 1523026071      1 1523026071 1523026518   TRUE
17  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE
18  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE
19  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE
20  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE
21  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE
22  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE
23  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE
24  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE
25  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE
26  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE
27  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE
28  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE
29  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE
30  2018-04-06 14:47:52 1523026072      1 1523026071 1523026518   TRUE

基础 R:

df$DIVE <- df$dive_start <= df$epoch & df$epoch <= df$dive_end

tidyverse 风格:

library(dplyr)
df %>% 
    mutate(DIVE = between(epoch, dive_start, dive_end)

tidyverse 方式...

library(dplyr)
df %>% mutate(DIVE = epoch >= dive_start & epoch <= dive_end)