如果应用于按日期范围定义的多行



此处为样本数据

dat <- structure(list(UserEmail = c("benradic@gmail.com", "gettrapped@optusnet.com.au", 
"bradley.grove1@three.com.au", "jimt@tadaust.org.au", "kmason@lakemac.nsw.gov.au", 
"Bstar@colyirr.com.au", "mulga@lizzy.com.au", "jodabry@bigpond.net.au", 
"sg_groups@freespiritresorts.com.au", "the.ranch@bigpond.com", 
"robhall@narrangullen.com.au", "muirriverstone@activ8.net.au", 
"benradic@gmail.com", "maryke@shopbasics.com.au", "hugh@feralanimalcontrol.com.au", 
"hugh@feralanimalcontrol.com.au", "hugh@feralanimalcontrol.com.au", 
"hugh@feralanimalcontrol.com.au", "hugh@feralanimalcontrol.com.au", 
"timothy.nalder@cma.nsw.gov.au", "hugh@feralanimalcontrol.com.au", 
"craig@tallawang.com", "nicole.urquhart@bigpond.com", "johan_ras@yahoo.com", 
"glenn.conroy@invasiveanimals.com", "deborahtaylor31@gmail.com", 
"the.spongos@bigpond.com", "mandgtill@bigpon.com", "sonley@alburycity.nsw.gov.au", 
"auctio@bigpond.com", "srstewart@bigpond.com", "pestcontrol.conservation@gmail.com", 
"p.m.worsley@gmail.com", "L.mulligan@bthstu.catholic.edu.au", 
"mickandlara@gmail.com", "mickandlara@gmail.com", "mickandlara@gmail.com", 
"ld@any1.ws", "vishusvb@gmail.com", "damianw1976@gmail.com", 
"peter.west.feralscan@dpi.nsw.gov.au", "una.greco@gmail.com", 
"peteles.fish@gmail.com", "greg.glasgow@trade.nsw.gov.au", "greg.glasgow@trade.nsw.gov.au", 
"greg.glasgow@trade.nsw.gov.au", "greg.glasgow@trade.nsw.gov.au", 
"greg.glasgow@trade.nsw.gov.au", "greg.glasgow@trade.nsw.gov.au", 
"greg.glasgow@trade.nsw.gov.au", "greg.glasgow@trade.nsw.gov.au", 
"sunnyside568@gmail.com", "una.greco@gmail.com", "greg.glasgow@trade.nsw.gov.au", 
"greg.glasgow@trade.nsw.gov.au", "helen@rpv.net.au", "gareth.debney@northsydney.nsw.gov.au", 
"gareth.debney@northsydney.nsw.gov.au", "gareth.debney@northsydney.nsw.gov.au", 
"Joanna.thomas1@outlook.com", "haydenfulthorpe@gmail.com", "Mwbuggy@bigpond.net.au", 
"Mwbuggy@bigpond.net.au", "Mwbuggy@bigpond.net.au", "Mwbuggy@bigpond.net.au", 
"antony.mulhall@skymesh.com.au", "peter.west.feralscan@dpi.nsw.gov.au", 
"peter.west.feralscan@dpi.nsw.gov.au", "sandranewby@southernphone.com.au", 
"dlucy@bigpond.net.au", "greg.glasgow@trade.nsw.gov.au", "alexbertolla@hotmail.com", 
"samuel.porter@portstephens.nsw.gov.au", "samuel.porter@portstephens.nsw.gov.au", 
"samuel.porter@portstephens.nsw.gov.au", "samuel.porter@portstephens.nsw.gov.au", 
"samuel.porter@portstephens.nsw.gov.au", "samuel.porter@portstephens.nsw.gov.au", 
"samuel.porter@portstephens.nsw.gov.au", "samuel.porter@portstephens.nsw.gov.au", 
"samuel.porter@portstephens.nsw.gov.au", "samuel.porter@portstephens.nsw.gov.au", 
"samuel.porter@portstephens.nsw.gov.au", "greg.glasgow@trade.nsw.gov.au", 
"Stealthgunsforhire@gmail.com", "Stealthgunsforhire@gmail.com", 
"greg.glasgow@trade.nsw.gov.au", "Stealthgunsforhire@gmail.com", 
"greg.glasgow@trade.nsw.gov.au", "greg.glasgow@trade.nsw.gov.au", 
"greg.glasgow@trade.nsw.gov.au", "una.greco@gmail.com", "Stealthgunsforhire@gmail.com", 
"greg.glasgow@trade.nsw.gov.au", "greg.glasgow@trade.nsw.gov.au", 
"cregan@bigpond.net.au", "dglwynn@westnet.com.au", "jcqgreig@gmail.com", 
"themkents1@gmail.com", "sue.wallace@snowy.net.au", "Stealthgunsforhire@gmail.com", 
"greg.glasgow@trade.nsw.gov.au", "pam.sheahan@gmail.com", "Stealthgunsforhire@gmail.com", 
"greg.glasgow@trade.nsw.gov.au", "Stealthgunsforhire@gmail.com", 
"Stealthgunsforhire@gmail.com", "philp@envite.org.au", "philp@envite.org.au", 
"greg.glasgow@trade.nsw.gov.au", "greg.glasgow@trade.nsw.gov.au", 
"greg.glasgow@trade.nsw.gov.au", "greg.glasgow@trade.nsw.gov.au", 
"Stealthgunsforhire@gmail.com", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"Jillian.macintyre@northernbeaches.nsw.gov.au", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"martin.azzopardi@resmed.com.au", "greg.glasgow@trade.nsw.gov.au", 
"mark.bosma@icloud.com", "Stealthgunsforhire@gmail.com", "tippettm6@gmail.com", 
"munmurra.pastoral@gmail.com", "jsacres@bigpond.com", "admin@adorealpacas.com.au", 
"munmurra.pastoral@gmail.com", "greg.glasgow@trade.nsw.gov.au", 
"greg.glasgow@trade.nsw.gov.au", "greg.glasgow@trade.nsw.gov.au", 
"Stealthgunsforhire@gmail.com", "Stealthgunsforhire@gmail.com", 
"Stealthgunsforhire@gmail.com", "Stealthgunsforhire@gmail.com", 
"federal@aapt.net.au", "rustee16@bigpond.com", "Stealthgunsforhire@gmail.com", 
"federal@aapt.net.au", "federal@aapt.net.au", "sue@peterirvine.com", 
"Stealthgunsforhire@gmail.com", "andy@agnov8.com", "andy@agnov8.com", 
"dandythomas@bigpond.com", "bellevue.deepwater@bigpond.com", 
"greg.glasgow@trade.nsw.gov.au", "leytonpayne8@gmail.com", "kade.small@lls.nsw.gov.au", 
"olivia.nougher@lls.nsw.gov.au", "greg.glasgow@trade.nsw.gov.au", 
"rudi.adlmayer@woollahra.nsw.gov.au", "sonja.elwood@northernbeaches.nsw.gov.au", 
"olivia.nougher@lls.nsw.gov.au", "Mjfitzgeraldnewcastle@hotmail.com", 
"stephanie.martin@environment.nsw.gov.au", "stephanie.martin@environment.nsw.gov.au", 
"stephanie.martin@environment.nsw.gov.au", "stephanie.martin@environment.nsw.gov.au", 
"stephanie.martin@environment.nsw.gov.au", "stephanie.martin@environment.nsw.gov.au", 
"sonja.elwood@northernbeaches.nsw.gov.au", "sonja.elwood@northernbeaches.nsw.gov.au", 
"sonja.elwood@northernbeaches.nsw.gov.au", "sonja.elwood@northernbeaches.nsw.gov.au", 
"sonja.elwood@northernbeaches.nsw.gov.au", "sonja.elwood@northernbeaches.nsw.gov.au", 
"sonja.elwood@northernbeaches.nsw.gov.au", "sonja.elwood@northernbeaches.nsw.gov.au", 
"sonja.elwood@northernbeaches.nsw.gov.au", "sonja.elwood@northernbeaches.nsw.gov.au", 
"sonja.elwood@northernbeaches.nsw.gov.au"), State = c("NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", 
"NSW"), date = structure(c(14853, 14883, 14975, 14975, 15006, 
15006, 15006, 15006, 15065, 15095, 15156, 15156, 15187, 15218, 
15248, 15309, 15309, 15340, 15340, 15340, 15371, 15371, 15431, 
15706, 15706, 15765, 15857, 15918, 16010, 16191, 16222, 16252, 
16283, 16344, 16375, 16375, 16375, 16436, 16526, 16617, 16617, 
16648, 16648, 16709, 16709, 16709, 16709, 16709, 16709, 16770, 
16770, 16770, 16770, 16770, 16801, 16801, 16832, 16832, 16832, 
16832, 16861, 16861, 16861, 16861, 16861, 16861, 16861, 16861, 
16892, 16922, 16922, 16922, 16953, 16953, 16953, 16953, 16953, 
16953, 16953, 16953, 16953, 16953, 16953, 16953, 16983, 16983, 
16983, 16983, 16983, 16983, 16983, 17014, 17014, 17014, 17014, 
17014, 17014, 17014, 17014, 17014, 17045, 17045, 17045, 17045, 
17045, 17045, 17045, 17045, 17045, 17045, 17045, 17045, 17045, 
17075, 17075, 17075, 17075, 17075, 17075, 17075, 17075, 17075, 
17075, 17075, 17075, 17075, 17075, 17075, 17075, 17075, 17075, 
17075, 17075, 17075, 17075, 17075, 17075, 17075, 17075, 17075, 
17075, 17075, 17075, 17075, 17075, 17075, 17075, 17106, 17106, 
17106, 17106, 17106, 17136, 17136, 17136, 17167, 17167, 17167, 
17167, 17167, 17167, 17167, 17198, 17198, 17198, 17198, 17198, 
17198, 17198, 17198, 17318, 17318, 17318, 17318, 17348, 17348, 
17348, 17379, 17379, 17379, 17410, 17410, 17440, 17440, 17440, 
17440, 17440, 17440, 17440, 17440, 17440, 17440, 17440, 17440, 
17440, 17440, 17440, 17440, 17440, 17440), class = "Date"), rhdv = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 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, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 
1, 0, 1, 1, 0, 1, 0, 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, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1), pindone = 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, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 1, 1, 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, 1, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 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)), row.names = c(NA, -200L), groups = structure(list(
UserEmail = c("admin@adorealpacas.com.au", "alexbertolla@hotmail.com", 
"andy@agnov8.com", "antony.mulhall@skymesh.com.au", "auctio@bigpond.com", 
"bellevue.deepwater@bigpond.com", "benradic@gmail.com", "bradley.grove1@three.com.au", 
"Bstar@colyirr.com.au", "craig@tallawang.com", "cregan@bigpond.net.au", 
"damianw1976@gmail.com", "dandythomas@bigpond.com", "deborahtaylor31@gmail.com", 
"dglwynn@westnet.com.au", "dlucy@bigpond.net.au", "federal@aapt.net.au", 
"gareth.debney@northsydney.nsw.gov.au", "gettrapped@optusnet.com.au", 
"glenn.conroy@invasiveanimals.com", "greg.glasgow@trade.nsw.gov.au", 
"haydenfulthorpe@gmail.com", "helen@rpv.net.au", "hugh@feralanimalcontrol.com.au", 
"jcqgreig@gmail.com", "Jillian.macintyre@northernbeaches.nsw.gov.au", 
"jimt@tadaust.org.au", "Joanna.thomas1@outlook.com", "jodabry@bigpond.net.au", 
"johan_ras@yahoo.com", "jsacres@bigpond.com", "kade.small@lls.nsw.gov.au", 
"kmason@lakemac.nsw.gov.au", "L.mulligan@bthstu.catholic.edu.au", 
"ld@any1.ws", "leytonpayne8@gmail.com", "mandgtill@bigpon.com", 
"mark.bosma@icloud.com", "martin.azzopardi@resmed.com.au", 
"maryke@shopbasics.com.au", "mickandlara@gmail.com", "Mjfitzgeraldnewcastle@hotmail.com", 
"muirriverstone@activ8.net.au", "mulga@lizzy.com.au", "munmurra.pastoral@gmail.com", 
"Mwbuggy@bigpond.net.au", "nicole.urquhart@bigpond.com", 
"olivia.nougher@lls.nsw.gov.au", "p.m.worsley@gmail.com", 
"pam.sheahan@gmail.com", "pestcontrol.conservation@gmail.com", 
"peteles.fish@gmail.com", "peter.west.feralscan@dpi.nsw.gov.au", 
"philp@envite.org.au", "robhall@narrangullen.com.au", "rudi.adlmayer@woollahra.nsw.gov.au", 
"rustee16@bigpond.com", "samuel.porter@portstephens.nsw.gov.au", 
"sandranewby@southernphone.com.au", "sg_groups@freespiritresorts.com.au", 
"sonja.elwood@northernbeaches.nsw.gov.au", "sonley@alburycity.nsw.gov.au", 
"srstewart@bigpond.com", "Stealthgunsforhire@gmail.com", 
"stephanie.martin@environment.nsw.gov.au", "sue.wallace@snowy.net.au", 
"sue@peterirvine.com", "sunnyside568@gmail.com", "the.ranch@bigpond.com", 
"the.spongos@bigpond.com", "themkents1@gmail.com", "timothy.nalder@cma.nsw.gov.au", 
"tippettm6@gmail.com", "una.greco@gmail.com", "vishusvb@gmail.com"
), .rows = structure(list(155L, 72L, 171:172, 66L, 30L, 174L, 
c(1L, 13L), 3L, 6L, 22L, 96L, 40L, 173L, 26L, 97L, 70L, 
c(164L, 167L, 168L), 57:59, 2L, 25L, c(44L, 45L, 46L, 
47L, 48L, 49L, 50L, 51L, 54L, 55L, 71L, 84L, 87L, 89L, 
90L, 91L, 94L, 95L, 102L, 105L, 110L, 111L, 112L, 113L, 
149L, 157L, 158L, 159L, 175L, 179L), 61L, 56L, c(15L, 
16L, 17L, 18L, 19L, 21L), 98L, 115:147, 4L, 60L, 8L, 
24L, 154L, 177L, 5L, 34L, 38L, 176L, 28L, 150L, 148L, 
14L, 35:37, 183L, 12L, 7L, c(153L, 156L), 62:65, 23L, 
c(178L, 182L), 33L, 103L, 32L, 43L, c(41L, 67L, 68L), 
108:109, 11L, 180L, 165L, 73:83, 69L, 9L, c(181L, 190L, 
191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L, 
200L), 29L, 31L, c(85L, 86L, 88L, 93L, 101L, 104L, 106L, 
107L, 114L, 151L, 160L, 161L, 162L, 163L, 166L, 170L), 
184:189, 100L, 169L, 52L, 10L, 27L, 99L, 20L, 152L, c(42L, 
53L, 92L), 39L), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, 75L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

问题:对于dat$UserEmail的唯一分组中的每个dat$rhdv记录,我想确定dat$rhdv记录日期两侧的12个月窗口内的所有实例,其中dat$pindone == 1。我正在寻找的解决方案是附加到dat,dat$pindone12的新列,如果上述为假,则指示01,如果上述为真(即如果为真,则应该在dat$rhdv == 1记录之前或之后的12个月内发生dat$pindone == 1记录,用于特定的dat$UserEmail)。

迄今为止,我已经尝试了多次解决方案,但没有任何成功。我怀疑解决方案的开始是

dat$pindone12 <- dat %>% group_by(UserEmail) %>% ifelse(rhdv == 1 & *condition to refer to required date range across any rows* & pindone == 1, 1, 0) 

将其分成几个步骤可能更容易,即计算出"rhdv - 12mo"one_answers"rhdv + 12mo",然后检查pindone是否在时间范围内。不确定此解决方案是否适用于您的实际数据,但也许:

library(tidyverse)
library(lubridate)
dat2 <- dat %>%
group_by(UserEmail) %>%
mutate(Start_date = ifelse(rhdv == 1, date - years(1), NA),
End_date = ifelse(rhdv == 1, date + years(1), NA)) %>%
mutate(Start_date = as.Date(Start_date, origin = "1970-01-01"),
End_date = as.Date(End_date, origin = "1970-01-01")) %>%
fill(c(Start_date, End_date), .direction = "downup") %>%
mutate(pindone12 = ifelse(date > Start_date &
date < End_date &
pindone == 1,
1, 0))
dat2 %>%
arrange(desc(pindone12))
#> # A tibble: 200 × 8
#> # Groups:   UserEmail [75]
#>    UserEmail      State date        rhdv pindone Start_date End_date   pindone12
#>    <chr>          <chr> <date>     <dbl>   <dbl> <date>     <date>         <dbl>
#>  1 samuel.porter… NSW   2016-06-01     0       1 2015-06-01 2017-06-01         1
#>  2 samuel.porter… NSW   2016-06-01     1       1 2015-06-01 2017-06-01         1
#>  3 samuel.porter… NSW   2016-06-01     0       1 2015-06-01 2017-06-01         1
#>  4 samuel.porter… NSW   2016-06-01     0       1 2015-06-01 2017-06-01         1
#>  5 samuel.porter… NSW   2016-06-01     0       1 2015-06-01 2017-06-01         1
#>  6 samuel.porter… NSW   2016-06-01     0       1 2015-06-01 2017-06-01         1
#>  7 benradic@gmai… NSW   2010-09-01     0       0 NA         NA                 0
#>  8 gettrapped@op… NSW   2010-10-01     0       0 NA         NA                 0
#>  9 bradley.grove… NSW   2011-01-01     0       0 NA         NA                 0
#> 10 jimt@tadaust.… NSW   2011-01-01     0       0 NA         NA                 0
#> # … with 190 more rows

由reprex包(v2.0.1)创建于2022-06-21

这显示了一个UserEmail,有6条pindone12记录;这就是你想看到的吗?

jared_mamrot解决方案看起来不错。我读你的指示:在每组UserEmail中,如果一个记录有rhdv ==1,在该日期的一年内找到记录,如果任何其他记录有pindone ==1,该特定记录得到pindone12 =1,否则为0。我认为jared的解决方法是给你pindone12 = 1如果pindone = 1。此外,他的解决方案是为rhdv == 0的记录赋予pindon12 = 1。如果这是你想要的,很好。根据我的理解:

library(purrr)
library(dplyr)
# Group and nest data
dat %>% 
group_by(UserEmail) %>% 
nest() %>% 
# pmap to retain UserEmail column
purrr::pmap_dfr( function(UserEmail, data){
data %>% 
# tmp data to index within each group
{. -> tmp
lapply(1:nrow(tmp), function(i){
# If more than one record, otherwise pindone12 is 0 because no other records with pindone == 1
if(nrow(tmp) > 1) {
tmp %>% 
# For each record get other records within one year
filter(date <= (date[i] +365) & date >= (date[i] - 365)) %>% 
# Find other records that have pindone == 1 and that record has rhdv == 1
dplyr::mutate(pindone12 = ifelse(any(pindone[-i] == 1 & rhdv[i] == 1), 1, 0),
email = UserEmail) %>% 
# Retain that one record
dplyr::slice(i)
} else {
tmp %>% 
mutate(pindone12 = 0,
email = UserEmail)
}
})
} %>% 
bind_rows
})

当我用pindone12 == 1筛选这些记录时

... %>% filter(pindone12 == 1)
# A tibble: 6 × 6
State date        rhdv pindone pindone12 email                             
<chr> <date>     <dbl>   <dbl>     <dbl> <chr>                             
1 NSW   2016-06-01     1       1         1 samuel.porter@portstephens.nsw.go…
2 NSW   2016-06-01     1       0         1 samuel.porter@portstephens.nsw.go…
3 NSW   2016-06-01     1       0         1 samuel.porter@portstephens.nsw.go…
4 NSW   2016-06-01     1       0         1 samuel.porter@portstephens.nsw.go…
5 NSW   2016-06-01     1       0         1 samuel.porter@portstephens.nsw.go…
6 NSW   2016-06-01     1       0         1 samuel.porter@portstephens.nsw.go…

相关内容

  • 没有找到相关文章