I have a somewhat slow solution for a problem that I encounter (in variations) more and more frequently now. I suspect there's a more efficient way of doing it and would love some pointers.

The toy example I created below doesn't take *that* long, but when I use several such lookup functions on my real data, it can take much, much longer.
Basically the intent is to count siblings that meet a number of conditions by group. Because it's dependent on the time that everybody was alive, it isn't the same result for every sibling.

```
library(dplyr)
# sample data
sibs = tbl_df(data.frame(survive1y = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 
1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 
1, 1, 0, 0, 1, 0, 1), byear = c(1717L, 1719L, 1721L, 1723L, 1724L, 
1725L, 1727L, 1728L, 1730L, 1732L, 1733L, 1735L, 1736L, 1738L, 
1740L, 1740L, 1742L, 1738L, 1744L, 1746L, 1748L, 1749L, 1753L, 
1755L, 1757L, 1758L, 1759L, 1761L, 1762L, 1764L, 1767L, 1717L, 
1719L, 1721L, 1786L, 1773L, 1767L, 1768L, 1792L), dyear = c(1748L, 
1791L, 1760L, 1795L, 1765L, 1756L, 1730L, 1733L, 1733L, 1732L, 
1755L, 1800L, 1736L, 1738L, 1740L, 1740L, 1761L, 1816L, 1744L, 
1748L, 1748L, 1749L, 1754L, 1756L, 1757L, 1759L, 1815L, 1761L, 
1765L, 1783L, 1768L, 1800L, 1750L, 1757L, 1786L, 1773L, 1769L, 
1768L, 1793L)))
sibs = bind_rows(replicate(10000, sibs, simplify = F))
sibs$idParents = rep(1:(nrow(sibs)/10), each = 10, length.out = nrow(sibs))

# get the number of siblings who were alive and dependent 
# in the first five years of this individual
dependent_sibs_f5y = function(survive1y, byear, dyear) {
    sibs = length(byear)
    other_dependent_sibs_f5y = integer(length=sibs)
    for(i in 1:sibs) {
        # remove this sib
        other_births = byear[-i]
        other_deaths = dyear[-i]
        other_made1y = survive1y[-i]
        my_sibs = sibs - 1 - # minus self
            sum(
                other_births > (byear[i] + 5) | # born more than 5y later
                (other_births + 5) < byear[i] | # finished infancy before birth
                other_deaths <= byear[i] | # died before birth
                other_made1y == 0, # if they died right away, don't count
            na.rm=T)  # if dyear missing assume they lived
        other_dependent_sibs_f5y[i] = my_sibs
    }
    other_dependent_sibs_f5y
}

system.time({
sibs2 = sibs %>%
   group_by(idParents) %>%
   mutate(
       dependent_sibs_f5y = 
       dependent_sibs_f5y(survive1y=survive1y, byear=byear, dyear=dyear)
   )
 })

```