Find & categorize regarding Hours data

I have a data.frame that collects data related to flights: which plane, the area over which it flies and when it entries and exits that area under a certain date format (2016-07-20 21:20:27). What I would like the code to do for me, is to eliminate the flights that doesn´t take place a certain date (2016-07-21), that implies 3 possible circumstances:
a) Flight enters the area on date different from (2016-07-21) but exits on (2016-07-21) : this flight should NOT be eliminated from the data.frame.
b) Flight enters the area on (2016-07-21) and exits also on (2016-07-21) : this flight should NOT be eliminated from the data.frame.
c) Flight enters and exits the area on date different from (2016-07-21) : this flight should be eliminated from the data.frame.

In my data.frame there is one column that identifies the flight, the area, the date it enters and the date it exits. I show a short reprex of how I have structured the data.frame.

Flight_Info <- data.frame(stringsAsFactors=FALSE,
Flight = c(1, 2, 3),
Area = c("A", "B", "C"),
Date_IN = c("2016-07-20 00:20:27", "2016-07-21 00:20:27",
"2016-07-20 00:20:27"),
Date_OUT = c("2016-07-21 21:20:27", "2016-07-21 21:20:27",
"2016-07-20 21:20:27")
)

In the example, the 3 possibilities are shown. As can be seen, flight 1 and 2 should remain in the data.frame but flight 3 shouldn't as it satisfies possibility c)

Thanks in advance for your help!

I implemented the rule that if both the date IN and the date Out are not 2016-07-21, then the data should be removed. Your three rules could be stated as "keep flights where the Out date is 2016-07-16", which would be simpler.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
Flight_Info <- data.frame(stringsAsFactors=FALSE,
                          Flight = c(1, 2, 3),
                          Area = c("A", "B", "C"),
                          Date_IN = c("2016-07-20 00:20:27", "2016-07-21 00:20:27",
                                      "2016-07-20 00:20:27"),
                          Date_OUT = c("2016-07-21 21:20:27", "2016-07-21 21:20:27",
                                       "2016-07-20 21:20:27")
)
Flight_Info2 <- Flight_Info %>% mutate(IN_Day = as.Date(Date_IN), OUT_Day = as.Date(Date_OUT)) %>% 
  filter(!(IN_Day != as.Date("2016-07-21") & OUT_Day != as.Date("2016-07-21"))) %>% 
  select(Flight, Area, Date_IN, Date_OUT)
Flight_Info2
#>   Flight Area             Date_IN            Date_OUT
#> 1      1    A 2016-07-20 00:20:27 2016-07-21 21:20:27
#> 2      2    B 2016-07-21 00:20:27 2016-07-21 21:20:27

Created on 2019-12-15 by the reprex package (v0.2.1)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.