I have one data frame containing event date by id:

data.frame(id = c("a", "a", "a", "d", "d"),
           date = as.Date(c("2018-01-03", "2018-02-02", "2018-02-22", "2018-02-13", "2018-05-01")))

  id       date
1  a 2018-01-03
2  a 2018-02-02
3  a 2018-02-22
4  d 2018-02-13
5  d 2018-05-01

And another one containing start and end of periods by id:

data.frame(id = c("a", "a", "d", "d", "d", "d"),
           start = as.Date(c("2018-01-15", "2018-01-30", "2018-03-01", "2018-02-01", "2018-04-02", "2018-03-19")),
           end = as.Date(c("2018-01-18", "2018-02-10", "2018-03-03", "2018-04-22", "2018-05-23", "2018-08-29")))

  id      start        end
1  a 2018-01-15 2018-01-18
2  a 2018-01-30 2018-02-10
3  d 2018-03-01 2018-03-03
4  d 2018-02-01 2018-04-22
5  d 2018-04-02 2018-05-23
6  d 2018-03-19 2018-08-29

For each id, I need to count the number of periods from the second data frame to which each date in the first data frame belongs.

My desired dataframe would be:

  id       date n
1  a 2018-01-03 0    # does not belong to any period
2  a 2018-02-02 1    # belongs to [2018-01-30,2018-02-10]
3  a 2018-02-22 0    # does not belong to any period
4  d 2018-02-13 1    # belongs to [2018-02-01,2018-04-22]
5  d 2018-05-01 2    # belongs to [2018-04-02,2018-05-23] and [2018-03-19,2018-08-29]

My problem is not about date comparison and summing the results. My problem is about performing those analysis inside each id group. I guess there is a way using split and/or the apply family, but I did not find how.

How can I do it in base R? I work in a restrictive environment where I only have access to base R.

2 Answers

1
St├ęphane Laurent On Best Solutions

Another base R approach:

dates <- data.frame(id = c("a", "a", "a", "d", "d"),
                    date = as.Date(c("2018-01-03", "2018-02-02", "2018-02-22", "2018-02-13", "2018-05-01")))
periods <- data.frame(id = c("a", "a", "d", "d", "d", "d"),
                      start = as.Date(c("2018-01-15", "2018-01-30", "2018-03-01", "2018-02-01", "2018-04-02", "2018-03-19")),
                      end = as.Date(c("2018-01-18", "2018-02-10", "2018-03-03", "2018-04-22", "2018-05-23", "2018-08-29")))

df <- transform(merge(dates, periods), belongs = date >= start & date <= end)

aggregate(belongs ~ date + id, data = df, sum)
#         date id belongs
# 1 2018-01-03  a       0
# 2 2018-02-02  a       1
# 3 2018-02-22  a       0
# 4 2018-02-13  d       1
# 5 2018-05-01  d       2

Or using data.table:

library(data.table)
dt <- as.data.table(merge(dates, periods))
dt[, .(n = sum(date >= start & date <= end)), by=c("id","date")]
#    id       date n
# 1:  a 2018-01-03 0
# 2:  a 2018-02-02 1
# 3:  a 2018-02-22 0
# 4:  d 2018-02-13 1
# 5:  d 2018-05-01 2
3
Wimpel On

base r approach

temp <- subset( merge(df1, df2), date >= start & date <= end, select = "date" )
df1$n <- sapply( df1$date, function(x) length( temp$date[ temp$date == x ] ))

#   id       date n
# 1  a 2018-01-03 0
# 2  a 2018-02-02 1
# 3  a 2018-02-22 0
# 4  d 2018-02-13 1
# 5  d 2018-05-01 2