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

发布于 2025-01-18 12:07:21 字数 5543 浏览 2 评论 0原文

我试图填充我在数据框“潜水”中创建的列,而true或false则基于数据框架中两个列的数字是否位于数字之间。 如果时期的数字介于dive_start和dive_end之间,那么我希望潜水等于true。如果它不介于dive_start和dive_end之间,我希望潜水等于false。 我尝试了基于类似的Stackoverflow帖子的尝试,但没有成功。因此,我想我只是问我的问题,看看我是否缺少任何东西。

这是数据的样子(在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 stackoverflows, 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"
    )
  ))

在我提供的示例数据中,如果代码有效,则在潜水柱中,所有NA值都将变为真实。但是,由于我列出了我尝试过的所有选项,因此它们将值留下了NA,通常我必须阻止代码继续运行,因为它不起作用,只是继续运行。我必须缺少某些内容或不正确编写代码。

预期输出:

            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

I am trying to fill a column I have created in my dataframe "DIVE" with True or False based off of if a number lies between the numbers of two columns in the dataframe.
If the epoch number falls between dive_start and dive_end, then I want DIVE to equal TRUE. If it does not fall between dive_start and dive_end, I want DIVE to equal FALSE.
I've tried this based off of similar stackoverflow posts, but with no success. So, I thought I'd just ask my question to see if there's anything I'm missing.

Here is what the data looks like (it was sampled at 16hz so it has repeated time stamps):

            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

Some things I've tried have not worked, here's what some of them are:

#I got these suggestions from other stackoverflows, 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"
    )
  ))

In the sample data I provided, if the code worked then all the NA values would become TRUE in the DIVE column. However, with all the options I listed that I've tried, they left the values NA and usually I had to stop the code from continuing to run because it wasn't working and just kept running. I must be missing something or not writing the code correctly.

Expected output:

            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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与风相奔跑 2025-01-25 12:07:22

整洁宇宙的方式...

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

The tidyverse way...

library(dplyr)
df %>% mutate(DIVE = epoch >= dive_start & epoch <= dive_end)
短叹 2025-01-25 12:07:22

基础 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)

base R:

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

tidyverse style:

library(dplyr)
df %>% 
    mutate(DIVE = between(epoch, dive_start, dive_end)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文