I need to filter my data based on two sets of conditions and dates: For the stations not including "150" in the station's name, I need to filter with start_date_AN and end_date_AN and for the stations inlcuding "150" in station's name, I need to filter with start_date and end_date.
I tried several options following other posts but I can't figure out how to do and I keep having errors I don't manage to solve (surely a comma or bracket missing).
Any ideas on how to do that?
Here is what I tried:
start_date <- as.Date("2025-03-01")
end_date <- as.Date("2025-07-01")
start_date_AN <- as.Date("2024-06-01")
end_date_AN <- as.Date("2025-06-01")
My script is used to create graph of the data. Of course I could filter within the graph section itself, but my idea is to modify only a few objects at the beginning of the script, so I would like to keep those lines.
Test 1
json <- json %>%
filter(case_when(!grepl("150", Station) ~ Date >= start_date_AN & Date < end_date_AN,
Date >= start_date & Date < end_date))
Test 2
json <- json %>%
filter(if (!grepl("150", Station) Date >= start_date_AN & Date < end_date_AN )
else grepl("150", Station) Date >= start_date & Date < end_date)
and a sample of my data:
test <- structure(list(measureDate = c("17/01/2025 04:00", "20/01/2025 04:00", "23/01/2025 04:00", "26/01/2025 04:00", "29/01/2025 04:00", "01/02/2025 04:00", "04/02/2025 04:00", "07/02/2025 04:00", "10/02/2025 04:00", "13/02/2025 04:00", "16/02/2025 04:00", "19/02/2025 04:00", "22/02/2025 04:00", "25/02/2025 04:00", "28/02/2025 04:00", "03/03/2025 04:00", "06/03/2025 04:00", "09/03/2025 04:00", "12/03/2025 04:00", "15/03/2025 04:00", "18/03/2025 04:00", "21/03/2025 04:00", "24/03/2025 04:00", "27/03/2025 04:00", "30/03/2025 05:00", "02/04/2025 05:00", "05/04/2025 05:00", "08/04/2025 05:00", "11/04/2025 05:00", "14/04/2025 05:00", "17/04/2025 05:00", "20/04/2025 05:00", "23/04/2025 05:00", "26/04/2025 05:00", "29/04/2025 05:00", "03/05/2025 05:00", "07/05/2025 05:00", "10/05/2025 05:00", "13/05/2025 05:00", "16/05/2025 05:00", "19/05/2025 05:00", "22/05/2025 05:00", "25/05/2025 05:00", "28/05/2025 05:00", "31/05/2025 05:00", "03/06/2025 05:00", "06/06/2025 05:00", "09/06/2025 05:00", "12/06/2025 05:00", "15/06/2025 05:00", "29/05/2024 22:00", "01/06/2024 22:00", "04/06/2024 22:00", "07/06/2024 22:00", "10/06/2024 22:00", "13/06/2024 22:00", "16/06/2024 22:00", "19/06/2024 22:00", "22/06/2024 22:00", "25/06/2024 22:00", "28/06/2024 22:00", "01/07/2024 22:00", "04/07/2024 22:00", "07/07/2024 22:00", "10/07/2024 22:00", "13/07/2024 22:00", "16/07/2024 22:00", "19/07/2024 22:00", "22/07/2024 22:00", "25/07/2024 22:00", "28/07/2024 22:00", "31/07/2024 22:00", "03/08/2024 22:00", "06/08/2024 22:00", "09/08/2024 22:00", "12/08/2024 22:00", "15/08/2024 22:00", "18/08/2024 22:00", "21/08/2024 22:00", "24/08/2024 22:00", "27/08/2024 22:00", "30/08/2024 22:00", "02/09/2024 22:00", "05/09/2024 22:00", "08/09/2024 22:00", "11/09/2024 22:00", "14/09/2024 22:00", "17/09/2024 22:00", "20/09/2024 22:00", "23/09/2024 22:00", "26/09/2024 22:00", "29/09/2024 22:00", "02/10/2024 22:00", "05/10/2024 22:00", "08/10/2024 22:00", "11/10/2024 22:00", "14/10/2024 22:00", "17/10/2024 22:00", "20/10/2024 22:00", "23/10/2024 22:00", "26/10/2024 22:00", "29/10/2024 21:00", "01/11/2024 21:00", "04/11/2024 21:00", "07/11/2024 21:00", "10/11/2024 21:00", "13/11/2024 21:00", "16/11/2024 21:00", "19/11/2024 21:00", "22/11/2024 21:00", "25/11/2024 21:00", "28/11/2024 21:00", "01/12/2024 21:00", "04/12/2024 21:00", "07/12/2024 21:00", "10/12/2024 21:00", "13/12/2024 21:00", "16/12/2024 21:00", "19/12/2024 21:00", "22/12/2024 21:00", "25/12/2024 21:00", "28/12/2024 21:00", "31/12/2025 21:00", "03/01/2025 21:00", "06/01/2025 21:00", "09/01/2025 21:00", "12/01/2025 21:00", "15/01/2025 21:00", "18/01/2025 21:00", "21/01/2025 21:00", "24/01/2025 21:00", "27/01/2025 21:00", "29/01/2025 23:00", "04/02/2025 21:00", "07/02/2025 21:00", "10/02/2025 21:00", "13/02/2025 21:00", "16/02/2025 21:00", "19/02/2025 21:00", "22/02/2025 21:00", "25/02/2025 21:00", "28/02/2025 21:00", "03/03/2025 21:00", "06/03/2025 21:00", "09/03/2025 21:00", "12/03/2025 21:00", "15/03/2025 21:00", "18/03/2025 21:00", "21/03/2025 21:00", "24/03/2025 21:00", "27/03/2025 21:00", "30/03/2025 22:00", "02/04/2025 22:00", "05/04/2025 22:00", "08/04/2025 22:00", "11/04/2025 22:00", "14/04/2025 22:00", "17/04/2025 22:00", "20/04/2025 22:00", "23/04/2025 22:00", "26/04/2025 22:00", "29/04/2025 22:00", "03/05/2025 22:00", "07/05/2025 22:00", "10/05/2025 22:00", "13/05/2025 22:00", "16/05/2025 22:00", "19/05/2025 22:00", "24/05/2025 22:00", "27/05/2025 22:00", "30/05/2025 22:00", "02/06/2025 22:00", "05/06/2025 22:00", "08/06/2025 22:00", "11/06/2025 22:00", "14/06/2025 22:00"), measure = c(1.282, 15.068, 19.36, 5.368, 17.037, 86.419, 46.032, 19.786, 12.032, 18.517, 4.15, 9.805, 1.518, 7.82, 11.866, 7.195, 3.48, 0.849, 36.306, 37.793, 16.217, 4.879, 42.244, 36.68, 9.311, 8.958, 5.569, 11.97, 7.044, 11.227, 68.627, 17.436, 26.215, 17.867, 7.574, 2.173, 24.009, 1.606, 9.062, 7.506, 1.461, 7.81, 1.623, 9.152, 1.395, 8.991, 5.111, 1.391, 2.26, 1.127, 17.929, 12.238, 12.721, 13.079, 13.096, 12.842, 13.367, 13.379, 13.65, 12.883, 14.262, 14.288, 14.121, 14.442, 15.017, 14.904, 15.296, 14.612, 14.962, 15.508, 15.654, 15.833, 15.963, 16.025, 16.113, 15.917, 15.192, 15.267, 15.438, 15.642, 15.442, 15.454, 15.438, 13.663, 13.742, 13.442, 12.117, 12.329, 12.658, 13.313, 12.196, 11.483, 12.996, 11.833, 12.379, 10.892, 11.579, 12.121, 11.421, 11.517, 11.692, 11.237, 11.05, 11.021, 11.254, 11.054, 10.362, 10.083, 10.483, 10.133, 10.725, 10.362, 9.746, 9.892, 9.796, 9.442, 9.667, 9.088, 9.933, 8.979, 8.367, 8.517, 8.742, 9.441, 10.059, 10.159, 9.447, 8.424, 8.676, 9.4, 9.818, 10.247, 9.3, 9.513, 9.306, 10.047, 10.176, 9.453, 9.982, 10.4, 10.512, 10.129, 9.724, 9.835, 10.218, 9.994, 10.2, 9.688, 10.606, 10.212, 10.4, 10.858, 10.547, 11.482, 11.112, 11.924, 11.7, 10.582, 10.947, 11.635, 11.794, 12.171, 11.912, 11.653, 12.194, 12.024, 11.806, 12.747, 11.618, 12.753, 13.641, 13.594, 13.447, 14.476, 14.788, 15.112), Station = c("Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y553571301_4_5_150_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24", "Y561621902_4_7_450_24"), DateH = structure(c(1737086400, 1737345600, 1737604800, 1737864000, 1738123200, 1738382400, 1738641600, 1738900800, 1739160000, 1739419200, 1739678400, 1739937600, 1740196800, 1740456000, 1740715200, 1740974400, 1741233600, 1741492800, 1741752000, 1742011200, 1742270400, 1742529600, 1742788800, 1743048000, 1743310800, 1743570000, 1743829200, 1744088400, 1744347600, 1744606800, 1744866000, 1745125200, 1745384400, 1745643600, 1745902800, 1746248400, 1746594000, 1746853200, 1747112400, 1747371600, 1747630800, 1747890000, 1748149200, 1748408400, 1748667600, 1748926800, 1749186000, 1749445200, 1749704400, 1749963600, 1717020000, 1717279200, 1717538400, 1717797600, 1718056800, 1718316000, 1718575200, 1718834400, 1719093600, 1719352800, 1719612000, 1719871200, 1720130400, 1720389600, 1720648800, 1720908000, 1721167200, 1721426400, 1721685600, 1721944800, 1722204000, 1722463200, 1722722400, 1722981600, 1723240800, 1723500000, 1723759200, 1724018400, 1724277600, 1724536800, 1724796000, 1725055200, 1725314400, 1725573600, 1725832800, 1726092000, 1726351200, 1726610400, 1726869600, 1727128800, 1727388000, 1727647200, 1727906400, 1728165600, 1728424800, 1728684000, 1728943200, 1729202400, 1729461600, 1729720800, 1729980000, 1730235600, 1730494800, 1730754000, 1731013200, 1731272400, 1731531600, 1731790800, 1732050000, 1732309200, 1732568400, 1732827600, 1733086800, 1733346000, 1733605200, 1733864400, 1734123600, 1734382800, 1734642000, 1734901200, 1735160400, 1735419600, 1767214800, 1735938000, 1736197200, 1736456400, 1736715600, 1736974800, 1737234000, 1737493200, 1737752400, 1738011600, 1738191600, 1738702800, 1738962000, 1739221200, 1739480400, 1739739600, 1739998800, 1740258000, 1740517200, 1740776400, 1741035600, 1741294800, 1741554000, 1741813200, 1742072400, 1742331600, 1742590800, 1742850000, 1743109200, 1743372000, 1743631200, 1743890400, 1744149600, 1744408800, 1744668000, 1744927200, 1745186400, 1745445600, 1745704800, 1745964000, 1746309600, 1746655200, 1746914400, 1747173600, 1747432800, 1747692000, 1748124000, 1748383200, 1748642400, 1748901600, 1749160800, 1749420000, 1749679200, 1749938400), tzone = "UTC", class = c("POSIXct", "POSIXt")), Date = structure(c(20105, 20108, 20111, 20114, 20117, 20120, 20123, 20126, 20129, 20132, 20135, 20138, 20141, 20144, 20147, 20150, 20153, 20156, 20159, 20162, 20165, 20168, 20171, 20174, 20177, 20180, 20183, 20186, 20189, 20192, 20195, 20198, 20201, 20204, 20207, 20211, 20215, 20218, 20221, 20224, 20227, 20230, 20233, 20236, 20239, 20242, 20245, 20248, 20251, 20254, 19872, 19875, 19878, 19881, 19884, 19887, 19890, 19893, 19896, 19899, 19902, 19905, 19908, 19911, 19914, 19917, 19920, 19923, 19926, 19929, 19932, 19935, 19938, 19941, 19944, 19947, 19950, 19953, 19956, 19959, 19962, 19965, 19968, 19971, 19974, 19977, 19980, 19983, 19986, 19989, 19992, 19995, 19998, 20001, 20004, 20007, 20010, 20013, 20016, 20019, 20022, 20025, 20028, 20031, 20034, 20037, 20040, 20043, 20046, 20049, 20052, 20055, 20058, 20061, 20064, 20067, 20070, 20073, 20076, 20079, 20082, 20085, 20453, 20091, 20094, 20097, 20100, 20103, 20106, 20109, 20112, 20115, 20117, 20123, 20126, 20129, 20132, 20135, 20138, 20141, 20144, 20147, 20150, 20153, 20156, 20159, 20162, 20165, 20168, 20171, 20174, 20177, 20180, 20183, 20186, 20189, 20192, 20195, 20198, 20201, 20204, 20207, 20211, 20215, 20218, 20221, 20224, 20227, 20232, 20235, 20238, 20241, 20244, 20247, 20250, 20253), class = "Date"), Month = structure(c(5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 7L, 7L, 7L, 7L, 7L, 8L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 7L, 7L, 7L, 7L, 7L), levels = c("août", "avril", "décembre", "février", "janvier", "juillet", "juin", "mai", "mars", "novembre", "octobre", "septembre"), class = "factor")), row.names = c(NA, -176L), class = "data.frame")
measure,Station, andDateHin conjunction withstart_date,start_date_AN& etc.. Desired output is generally useful, though perhaps is where the errors arise.