条件语句,基于R中的两列的值范围
我试图填充我在数据框“潜水”中创建的列,而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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
整洁宇宙的方式...
The tidyverse way...
基础 R:
tidyverse 风格:
base R:
tidyverse style: