r – if() statements within nested for() loops

I’m trying to loop through my data frame to identify a date period for each record/row, then loop back through my data frame to identify every other row that falls within this date period, and finally output this as a new data frame. However, I am having trouble with the nested for() loops and am new to writing loops generally. Any help/advice appreciated.

Sample data

dat1 <- structure(list(UserEmail = c("dlhandscombe@bigpond.com", "dlhandscombe@bigpond.com", 
"dlhandscombe@bigpond.com", "dlhandscombe@bigpond.com", "dlhandscombe@bigpond.com", 
"dlhandscombe@bigpond.com", "dlhandscombe@bigpond.com", "dlhandscombe@bigpond.com", 
"dlhandscombe@bigpond.com", "dlhandscombe@bigpond.com", "dlhandscombe@bigpond.com", 
"dlhandscombe@bigpond.com", "dlhandscombe@bigpond.com", "dlhandscombe@bigpond.com", 
"john.myles@sujo.com.au", "mcaaronnab@gmail.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "data@phbg.org", "data@phbg.org", 
"data@phbg.org", "data@phbg.org", "data@phbg.org", "data@phbg.org", 
"data@phbg.org", "data@phbg.org", "data@phbg.org", "data@phbg.org", 
"data@phbg.org", "data@phbg.org", "data@phbg.org", "data@phbg.org", 
"data@phbg.org", "data@phbg.org", "data@phbg.org", "rickmiddag@gmail.com", 
"gordonheron@hotmail.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"dlhandscombe@bigpond.com", "dlhandscombe@bigpond.com", "dlhandscombe@bigpond.com", 
"allwrightag@bigpond.com", "data@phbg.org", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "rickmiddag@gmail.com", 
"rickmiddag@gmail.com", "rickmiddag@gmail.com", "rickmiddag@gmail.com", 
"rickmiddag@gmail.com", "rickmiddag@gmail.com", "amjkbaker@hotmail.com", 
"amjkbaker@hotmail.com", "amjkbaker@hotmail.com", "amjkbaker@hotmail.com", 
"amjkbaker@hotmail.com", "signsonthehill@westvic.com.au", "signsonthehill@westvic.com.au", 
"signsonthehill@westvic.com.au", "signsonthehill@westvic.com.au", 
"rickmiddag@gmail.com", "rickmiddag@gmail.com", "rickmiddag@gmail.com", 
"rickmiddag@gmail.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"peter.west@dpi.nsw.gov.au", "peter.west@dpi.nsw.gov.au", "peter.west@dpi.nsw.gov.au", 
"rickmiddag@gmail.com", "dlhandscombe@bigpond.com", "dlhandscombe@bigpond.com", 
"dlhandscombe@bigpond.com", "dlhandscombe@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "gordonheron@hotmail.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com", "allwrightag@bigpond.com", "allwrightag@bigpond.com", 
"allwrightag@bigpond.com"), State = c("VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "WA", "NSW", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "WA", "WA", "WA", "WA", "WA", "WA", "WA", "WA", 
"WA", "WA", "WA", "WA", "WA", "WA", "WA", "WA", "WA", "VIC", 
"WA", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "WA", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "NSW", "NSW", "NSW", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "WA", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", "VIC", 
"VIC", "VIC", "VIC", "VIC", "VIC", "VIC"), date = structure(c(18779, 
18779, 18779, 18779, 18779, 18779, 18779, 18779, 18779, 18779, 
18779, 18779, 18779, 18779, 18779, 18779, 18779, 18779, 18779, 
18779, 18779, 18779, 18779, 18779, 18779, 18779, 18779, 18779, 
18779, 18779, 18779, 18779, 18779, 18597, 18597, 18597, 18567, 
18567, 18567, 18567, 18567, 18779, 18567, 18567, 18567, 18567, 
18567, 18567, 18567, 18779, 18748, 18748, 18748, 18748, 18748, 
18748, 18748, 18748, 18748, 18748, 18748, 18748, 18748, 18748, 
18597, 18748, 18748, 18748, 18748, 18748, 18748, 18748, 18748, 
18748, 18748, 18748, 18748, 18748, 18748, 18748, 18748, 18748, 
18748, 18748, 18748, 18748, 18748, 18748, 18748, 18748, 18748, 
18748, 18748, 18748, 18748, 18748, 18748, 18748, 18748, 18748, 
18748, 18748, 18748, 18748, 18748, 18748, 18748, 18748, 18748, 
18748, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 
18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 
18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 
18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 
18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 
18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 
18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 
18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 
18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 
18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 18718, 
18718, 18718), class = "Date"), Latitude = c(-38.32750078, -38.32755782, 
-38.32770199, -38.32783086, -38.32792469, -38.32784058, -38.32688814, 
-38.32640275, -38.32680571, -38.32695654, -38.32189002, -38.32211243, 
-38.32186521, -38.32164195, -33.663844, -31.79630483, -38.20266026, 
-38.20449152, -38.20501353, -38.20557419, -38.20562426, -38.20551695, 
-38.2054608, -38.20532734, -38.20516026, -38.2053161, -38.20539147, 
-38.2053905, -38.2053045, -38.20557603, -38.20466756, -38.20497432, 
-38.20510728, -32.233408, -32.190961, -32.311607, -32.763257, 
-32.777375, -32.999771, -33.240519, -33.260944, -32.947448, -32.957423, 
-32.961487, -32.817692, -32.833525, -32.746101, -32.711097, -32.480902, 
-32.530879, -38.32679155, -32.25924559, -38.08784578, -38.08789701, 
-38.08788353, -38.08890263, -38.09758783, -38.09743259, -38.09765087, 
-38.05242984, -38.32194215, -38.32158475, -38.32156773, -38.27176766, 
-32.674967, -38.27983537, -38.27978588, -38.27972551, -38.28018098, 
-38.28094721, -38.28070268, -38.27945243, -38.27941373, -38.279413, 
-38.27929586, -38.27930325, -38.27935421, -38.27934516, -38.2792951, 
-38.27931194, -38.27924581, -38.27924407, -38.27926769, -38.27928767, 
-38.27709804, -38.27705115, -38.27688883, -38.27688606, -38.27680544, 
-38.27679706, -38.27670155, -38.27657823, -38.27421942, -38.27372957, 
-38.27213358, -38.32745008, -38.32727362, -38.32719026, -38.32719539, 
-38.32729929, -38.3270412, -38.32631352, -38.32643169, -38.32703682, 
-38.32672818, -38.32660818, -38.32485432, -38.32491437, -38.32513905, 
-38.3253491, -38.32764073, -38.32751707, -38.32729056, -38.32736647, 
-38.09894507, -38.09972331, -38.09804106, -38.09738829, -38.09721255, 
-38.09703156, -38.09714765, -38.09705681, -38.09665265, -38.09662643, 
-38.09664096, -38.09660886, -38.09675016, -38.09690748, -38.09682883, 
-38.09682355, -38.09664529, -38.09653774, -38.09660409, -38.0965979, 
-33.3213753, -33.32149898, -33.32065404, -38.32768748, -38.32407191, 
-38.32206629, -38.32196227, -38.32158098, -38.09912166, -38.09607056, 
-38.09603734, -38.09593104, -38.09591908, -38.09591603, -38.09595413, 
-38.09599572, -38.09602772, -38.09666616, -38.09648092, -38.09650212, 
-38.09647057, -38.096317, -38.09623704, -38.09634354, -38.09636717, 
-38.09643443, -38.0964877, -38.09661571, -38.09672981, -38.09786273, 
-38.09765968, -38.09769648, -32.25455, -38.0978205, -38.09741105, 
-38.09741489, -38.0972944, -38.09730579, -38.09734227, -38.09739487, 
-38.09966568, -38.09935175, -38.09855665, -38.09855142, -38.09856634, 
-38.09838202, -38.09833261, -38.09847139, -38.0990405, -38.09892204, 
-38.0989384, -38.09802511, -38.09739538, -38.09739113, -38.09739696, 
-38.09732931, -38.19417003, -38.19511853, -38.20266664, -38.20268123, 
-38.20256201, -38.19948944, -38.19929273, -38.19915499, -38.19890007, 
-38.20047247, -38.19930315), Longitude = c(142.3835597, 142.3835428, 
142.3835246, 142.3835401, 142.3835625, 142.3839238, 142.384328, 
142.3842913, 142.3844308, 142.3844157, 142.3846232, 142.3848707, 
142.3849729, 142.3853016, 115.071187, 152.5526075, 144.2969955, 
144.2746839, 144.2742302, 144.2759461, 144.2760525, 144.2763227, 
144.2762913, 144.2760796, 144.2763363, 144.275784, 144.2756106, 
144.275706, 144.2754788, 144.2751584, 144.2742694, 144.2746347, 
144.2750641, 115.964234, 115.930869, 115.978905, 115.687101, 
115.700409, 115.928638, 115.878337, 115.808685, 115.944362, 115.946851, 
115.777092, 115.901788, 115.820135, 115.665144, 116.091728, 115.776342, 
115.779243, 142.3754207, 116.5346159, 144.2686945, 144.268404, 
144.2683545, 144.2705866, 144.060009, 144.059962, 144.0600576, 
144.1638083, 142.3846214, 142.3819954, 142.3826656, 144.2678378, 
115.828611, 144.2602225, 144.2603233, 144.260455, 144.2597179, 
144.2586625, 144.2591689, 144.2668923, 144.2664998, 144.2660241, 
144.265639, 144.2654265, 144.2654584, 144.2653161, 144.2649785, 
144.2652768, 144.2648198, 144.2647389, 144.2648483, 144.2648925, 
144.2635063, 144.2635559, 144.2635468, 144.2636286, 144.2637379, 
144.2637387, 144.2637568, 144.2638504, 144.2658279, 144.2662149, 
144.2674783, 142.3765166, 142.3764392, 142.3764025, 142.3762023, 
142.3761641, 142.3766339, 142.3789152, 142.3789916, 142.3789607, 
142.3789572, 142.3789238, 142.3782066, 142.377991, 142.3779546, 
142.3778585, 142.3761033, 142.3761088, 142.376079, 142.3761394, 
144.0517027, 144.0526404, 144.0536347, 144.0537263, 144.0541184, 
144.0564165, 144.0564846, 144.0565053, 144.0573631, 144.0571971, 
144.0571328, 144.0571447, 144.0567521, 144.0566895, 144.0567403, 
144.0565734, 144.0566662, 144.0566595, 144.0567389, 144.0568193, 
149.085023, 149.0849412, 149.085214, 142.3763892, 142.3850872, 
142.3848902, 142.3854931, 142.3853472, 144.0508455, 144.0452349, 
144.0453495, 144.0458212, 144.0460047, 144.046109, 144.0465288, 
144.0466128, 144.0467943, 144.0455742, 144.0456054, 144.0452869, 
144.0449897, 144.0447057, 144.044721, 144.0469688, 144.0468794, 
144.0472526, 144.0472304, 144.0472068, 144.0472513, 144.0481364, 
144.048352, 144.0486572, 116.54061, 144.0483498, 144.0486064, 
144.0483553, 144.0487625, 144.0488145, 144.0490169, 144.0490671, 
144.0526365, 144.0527653, 144.0524581, 144.0524492, 144.0522597, 
144.0520267, 144.051803, 144.0517975, 144.0513376, 144.0513614, 
144.0516648, 144.0536805, 144.0537439, 144.0538747, 144.0537002, 
144.053566, 144.2989582, 144.288593, 144.2971804, 144.2971341, 
144.296884, 144.2978075, 144.3008932, 144.3006786, 144.2954975, 
144.2919441, 144.2909927), month = c("06", "06", "06", "06", 
"06", "06", "06", "06", "06", "06", "06", "06", "06", "06", "06", 
"06", "06", "06", "06", "06", "06", "06", "06", "06", "06", "06", 
"06", "06", "06", "06", "06", "06", "06", "12", "12", "12", "11", 
"11", "11", "11", "11", "06", "11", "11", "11", "11", "11", "11", 
"11", "06", "05", "05", "05", "05", "05", "05", "05", "05", "05", 
"05", "05", "05", "05", "05", "12", "05", "05", "05", "05", "05", 
"05", "05", "05", "05", "05", "05", "05", "05", "05", "05", "05", 
"05", "05", "05", "05", "05", "05", "05", "05", "05", "05", "05", 
"05", "05", "05", "05", "05", "05", "05", "05", "05", "05", "05", 
"05", "05", "05", "05", "05", "05", "05", "04", "04", "04", "04", 
"04", "04", "04", "04", "04", "04", "04", "04", "04", "04", "04", 
"04", "04", "04", "04", "04", "04", "04", "04", "04", "04", "04", 
"04", "04", "04", "04", "04", "04", "04", "04", "04", "04", "04", 
"04", "04", "04", "04", "04", "04", "04", "04", "04", "04", "04", 
"04", "04", "04", "04", "04", "04", "04", "04", "04", "04", "04", 
"04", "04", "04", "04", "04", "04", "04", "04", "04", "04", "04", 
"04", "04", "04", "04", "04", "04", "04", "04", "04", "04", "04", 
"04", "04", "04", "04", "04", "04", "04", "04", "04", "04"), 
    year = c(2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2020, 2020, 2020, 2020, 2020, 
    2020, 2020, 2020, 2021, 2020, 2020, 2020, 2020, 2020, 2020, 
    2020, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2020, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
    2021, 2021, 2021), rhdv = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0), control.any = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), start_date = structure(c(18414, 
    18414, 18414, 18414, 18414, 18414, 18414, 18414, 18414, 18414, 
    18414, 18414, 18414, 18414, 18414, 18414, 18414, 18414, 18414, 
    18414, 18414, 18414, 18414, 18414, 18414, 18414, 18414, 18414, 
    18414, 18414, 18414, 18414, 18414, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 18383, 18383, 
    18383, 18383, 18383, 18383, 18383, 18383, 18383, 18383, 18383, 
    18383, 18383, 18383, NA, 18383, 18383, 18383, 18383, 18383, 
    18383, 18383, 18383, 18383, 18383, 18383, 18383, 18383, 18383, 
    18383, 18383, 18383, 18383, 18383, 18383, 18383, 18383, 18383, 
    18383, 18383, 18383, 18383, 18383, 18383, 18383, 18383, 18383, 
    18383, 18383, 18383, 18383, 18383, 18383, 18383, 18383, 18383, 
    18383, 18383, 18383, 18383, 18353, 18353, 18353, 18353, 18353, 
    18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 
    18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 
    18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 
    18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 
    18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 
    18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 
    18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 
    18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 
    18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 18353, 
    18353, 18353, 18353, 18353, 18353), class = "Date"), end_date = structure(c(19144, 
    19144, 19144, 19144, 19144, 19144, 19144, 19144, 19144, 19144, 
    19144, 19144, 19144, 19144, 19144, 19144, 19144, 19144, 19144, 
    19144, 19144, 19144, 19144, 19144, 19144, 19144, 19144, 19144, 
    19144, 19144, 19144, 19144, 19144, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 19113, 19113, 
    19113, 19113, 19113, 19113, 19113, 19113, 19113, 19113, 19113, 
    19113, 19113, 19113, NA, 19113, 19113, 19113, 19113, 19113, 
    19113, 19113, 19113, 19113, 19113, 19113, 19113, 19113, 19113, 
    19113, 19113, 19113, 19113, 19113, 19113, 19113, 19113, 19113, 
    19113, 19113, 19113, 19113, 19113, 19113, 19113, 19113, 19113, 
    19113, 19113, 19113, 19113, 19113, 19113, 19113, 19113, 19113, 
    19113, 19113, 19113, 19113, 19083, 19083, 19083, 19083, 19083, 
    19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 
    19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 
    19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 
    19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 
    19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 
    19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 
    19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 
    19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 
    19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 19083, 
    19083, 19083, 19083, 19083, 19083), class = "Date")), row.names = 200:400, class = "data.frame")

This is my attempt at the solution

# Create new, empty data frame
dat2 <- data.frame()

dat2 <- for(i in 1:nrow(dat1))
{
  m = 0
  if(dat1$control.any == 1) {           #Condition that must be met
    start_date = dat1$start_date[i]     #If condition met read in start date and end date
    end_date = dat1$end_date[i]
    
    for(j in nrow(dat1))
    {
      record_date = dat1$date[j]       #Loop back through data and extract record date
      if(record_date > start_date & record_date < end_date)    #Test if record date falls within period defined by start and end date, and if so populate new data frame 
      m = m + 1
      {
        dat2$UserEmail1[m] <- dat1$UserEmail[i]
        dat2$State1[m] <- dat1$State[i]
        dat2$date1[m] <- dat1$date[i]
        dat2$Longitude1[m] <- dat1$Longitude[i]
        dat2$Latitude1[m] <- dat1$Latitude[i]
        dat2$month1[m] <- dat1$month[i]
        dat2$year1[m] <- dat1$year[i]
        dat2$rhdv1[m] <- dat1$rhdv[i]
        dat2$control.any1[m] <- dat1$control.any[i]
        dat2$start_date[m] <- start_date
        dat2$end_date[m] <- end_date
        
        dat2$UserEmail2[m] <- dat1$UserEmail[j]
        dat2$State2[m] <- dat1$State[j]
        dat2$date2[m] <- record_date
        dat2$Longitude2[m] <- dat1$Longitude[j]
        dat2$Latitude2[m] <- dat1$Latitude[j]
        dat2$month2[m] <- dat1$month[j]
        dat2$year2[m] <- dat1$year[j]
        dat2$rhdv2[m] <- dat1$rhdv[j]
        dat2$control.any2[m] <- dat1$control.any[j]
        
      }
    }
  }
}

I do not have a literal example of what the above final dat2 should look like but I have created a small example of what I am trying to do.

example.dat1 <- data.frame(date = as.Date(c("01/01/2022",
                                            "03/05/2022",
                                            "06/07/2022"), format="%d/%m/%Y"), 
                           Latitude = c(35, 36, 34),
                           Longitude = c(117, 114, 117),
                           rhdv = c(0, 1, 1),
                           control.any = c(1, 0, 0),
                           start.date = as.Date(c("01/01/2021",
                                                  "03/05/2021",
                                                  "06/07/2021"), format="%d/%m/%Y"), 
                           end.date = as.Date(c("01/01/2023",
                                                "03/05/2023",
                                                "06/07/2023"), format="%d/%m/%Y"))

example.dat2 <- data.frame(date1 = as.Date(c("01/01/2022",
                                            "01/01/2022"), format="%d/%m/%Y"), 
                           Latitude1 = c(35, 35),
                           Longitude1 = c(117, 117),
                           rhdv1 = c(0, 0),
                           control.any1 = c(1, 1),
                           start.date = as.Date(c("01/01/2021",
                                                  "01/01/2021"), format="%d/%m/%Y"), 
                           end.date = as.Date(c("01/01/2023",
                                                "01/01/2023"), format="%d/%m/%Y"),
                           date2 = as.Date(c("03/05/2022",
                                             "06/07/2022"), format="%d/%m/%Y"), 
                           Latitude2 = c(36, 34),
                           Longitude2 = c(114, 117),
                           rhdv2 = c(1, 1),
                           control.any2 = c(0, 0))

From within example.dat1 there is one record where control.any == 1 and for this record there were two other records in the dataset where date fell within the start and end date of the control.any == 1 record.

Leave a Comment