I'm trying to find the first date (per group) where there are records in one week as well as the next. Weeks don't start on Mondays but are defined as a seven-day period.

Assuming a date is the first day of the first week, I'm trying to test if the number of date records that fall in the second "week" is greater than one.

```
library(data.table)

dt=data.table(date=c(1,9,10,15,18,3,4,7,7,19,21,27),
              group=c(rep("a", 5), rep("b",7)))

> dt
    date group
 1:    1     a
 2:    9     a
 3:   10     a
 4:   15     a
 5:   18     a
 6:    3     b
 7:    4     b
 8:    7     b
 9:    7     b
10:   19     b
11:   21     b
12:   27     b

```

A for loop that would work on a data.frame looks like this:

```
df <- data.frame(dt)

for(i in 1:length(df$date)){
  df$count[i] <- sum(df$date >= df$date[i] + 7 &
  df$date < df$date[i] + 14 &
  df$group == df$group[i])
}

> df
   date group  count
1     1     a      2
2     9     a      1
3    10     a      1
4    15     a      0
5    18     a      0
6     3     b      0
7     4     b      0
8     7     b      1
9     7     b      1
10   19     b      1
11   21     b      0
12   27     b      0

```

The first date per group where the count is greater than 0 would give me the start date of the first week, i.e. 1 in group "a" and 7 in group "b".

My real data.table has over ten million rows, so ideally I'd like a function that is similar to the above for-loop, so I can do something like this:

```
dt[, date/sum(date), by=group]

```

The problem is I don't understand how to create a function with indexing that works for a data.table. Any help greatly appreciated.