Check if date is whtitin data range for each row

i have an sf which contains several columns and a lot of row, like this one but with more rows:
Cattura

I would like to check if acq_date is between start_date and end_date (row by row ) and therefore have an output where only the lines that are between start and stop date are present, adding the fit column as in the image above

I can't check if acq_date is between start and end date for each single row, but i can do it for the whole column and the result is incorrect. Having a column with true or false later allows me to select only the true ones and delete the rows with false.

I hope I have expressed the problem clearly, I apologize but I am new with r.

Thanks to those who want to help me

If you use dplyr::between, you can perform it rowwise:

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

sample_data <- tibble(
  acq_date = seq.Date(
    from = as.Date(x = "2015-01-03"),
    to = as.Date(x = "2015-01-07"),
    by = "2 day"
  ),
  start_date = as.Date(x = "2015-01-04"),
  end_date = as.Date(x = "2015-01-06")
)

sample_data
#> # A tibble: 3 x 3
#>   acq_date   start_date end_date  
#>   <date>     <date>     <date>    
#> 1 2015-01-03 2015-01-04 2015-01-06
#> 2 2015-01-05 2015-01-04 2015-01-06
#> 3 2015-01-07 2015-01-04 2015-01-06

sample_data %>%
  rowwise() %>%
  mutate(is_between = between(
    x = acq_date,
    left = start_date,
    right = end_date
  )) %>%
  ungroup()
#> # A tibble: 3 x 4
#>   acq_date   start_date end_date   is_between
#>   <date>     <date>     <date>     <lgl>     
#> 1 2015-01-03 2015-01-04 2015-01-06 FALSE     
#> 2 2015-01-05 2015-01-04 2015-01-06 TRUE      
#> 3 2015-01-07 2015-01-04 2015-01-06 FALSE

sample_data %>%
  rowwise() %>%
  filter(between(
    x = acq_date,
    left = start_date,
    right = end_date
  )) %>%
  ungroup()
#> # A tibble: 1 x 3
#>   acq_date   start_date end_date  
#>   <date>     <date>     <date>    
#> 1 2015-01-05 2015-01-04 2015-01-06

Created on 2021-04-13 by the reprex package (v2.0.0)

A base R version for creating new column:

sample_data <- data.frame(
    acq_date = seq.Date(
        from = as.Date(x = "2015-01-03"),
        to = as.Date(x = "2015-01-07"),
        by = "2 day"
    ),
    start_date = as.Date(x = "2015-01-04"),
    end_date = as.Date(x = "2015-01-06")
)

within(sample_data, {
  is_between <- do.call(what = function(x, lb, ub) {
    (lb <= x) & (x <= ub)
  }, args = list(acq_date, start_date, end_date))
})
#>     acq_date start_date   end_date is_between
#> 1 2015-01-03 2015-01-04 2015-01-06      FALSE
#> 2 2015-01-05 2015-01-04 2015-01-06       TRUE
#> 3 2015-01-07 2015-01-04 2015-01-06      FALSE

Created on 2021-04-13 by the reprex package (v2.0.0)

Hope this helps.

Alternatively this works without the need for rowwise():

library(dplyr)
sample_data %>% mutate(is_between = (acq_date >= start_date & acq_date <= end_date))

wow! you are fantastic ... I managed to solve my problem that had been blocking me for 2 days. Thanks and I hope to be able to reciprocate your precious contribution

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.