Compare date by group in two data frames in R

纵然是瞬间 提交于 2020-01-13 17:56:31

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/55043988/compare-date-by-group-in-two-data-frames-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!