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

Time:04-02

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

CodePudding user response:

The tidyverse way...

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

CodePudding user response:

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)
  • Related