r语言 - 如何通过查看停止和开始日期来查看客户是否有另一个特定订阅在较早的订阅之后开始?



我正在使用的数据帧包含订阅条目,其中包含订阅的开始和停止日期。用户可以拥有多行,因为他/她可以拥有或拥有多个订阅。我想知道某个订阅之后是否是另一个订阅。

我考虑过使用 for 循环,因为观测量不是特别高(大约 2000 年(。但是,我对这个主题的了解不是特别高,所以我无法创建一个。每个用户都有自己的 ID 代码。可以有不同类型的订阅。我已经为特定订阅创建了一个虚拟变量,我想检查它是否被跟进。

数据外观示例:

id startdate stopdate subscriptiontype
1   2013-05-01  2013-06-01  1
2   2010-05-02  2012-05-02  3
2   2013-05-02  2013-06-02  1
2   2013-07-23  2013-12-23  2
4   2008-05-02  2011-05-02  3
4   2013-05-04  2013-06-04  1

我想查看每个"id"是否有另一个订阅的开始日期在订阅类型 1 的停止日期之后。这可能吗?感谢您的阅读!

数据

structure(list(id = c(1, 2, 2, 2, 4, 4), startdate = structure(c(3L, 
2L, 4L, 6L, 1L, 5L), .Label = c("2008-05-02", "2010-05-02", "2013-05-01", 
"2013-05-02", "2013-05-04", "2013-07-23"), class = "factor"), 
stopdate = structure(c(3L, 2L, 4L, 6L, 1L, 5L), .Label = c("2011-05-02", 
"2012-05-02", "2013-06-01", "2013-06-02", "2013-06-04", "2013-12-23"
), class = "factor"), subscriptiontype = c(1, 3, 1, 2, 3, 
1)), class = "data.frame", row.names = c(NA, -6L))

我修改了你的数据,做了以下操作。对于每个组,我认为您要检查是否有任何订阅类型遵循订阅类型 1。首先,我将两列转换为上课日期,以防万一。然后,对于每个ID,我都运行逻辑检查。基本上,我问的是"以前的值是否在subscriptiontype1

library(dplyr)
library(lubridate)
mutate_at(mydf, vars(contains("date")),
.funs = list(~ymd(.))) %>% 
group_by(id) %>% 
mutate(check = lag(subscriptiontype) == 1)
id startdate  stopdate   subscriptiontype check
<int> <date>     <date>                <int> <lgl>
1     1 2013-05-01 2013-06-01                1 NA   
2     2 2010-05-02 2012-05-02                3 NA   
3     2 2013-05-02 2013-06-02                1 FALSE
4     2 2013-07-23 2013-12-23                2 TRUE 
5     4 2008-05-02 2011-05-02                3 NA   
6     4 2013-05-04 2013-06-04                1 FALSE
7     7 2018-01-01 2018-02-01                3 NA   
8     7 2018-03-01 2018-03-15                1 FALSE
9     7 2018-04-01 2018-05-15                4 TRUE 

数据

mydf <- structure(list(id = c(1L, 2L, 2L, 2L, 4L, 4L, 7L, 7L, 7L), startdate = c("2013-05-01", 
"2010-05-02", "2013-05-02", "2013-07-23", "2008-05-02", "2013-05-04", 
"2018-01-01", "2018-03-01", "2018-04-01"), stopdate = c("2013-06-01", 
"2012-05-02", "2013-06-02", "2013-12-23", "2011-05-02", "2013-06-04", 
"2018-02-01", "2018-03-15", "2018-05-15"), subscriptiontype = c(1L, 
3L, 1L, 2L, 3L, 1L, 3L, 1L, 4L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9"))
id  startdate   stopdate subscriptiontype
1  1 2013-05-01 2013-06-01                1
2  2 2010-05-02 2012-05-02                3
3  2 2013-05-02 2013-06-02                1
4  2 2013-07-23 2013-12-23                2
5  4 2008-05-02 2011-05-02                3
6  4 2013-05-04 2013-06-04                1
7  7 2018-01-01 2018-02-01                3
8  7 2018-03-01 2018-03-15                1
9  7 2018-04-01 2018-05-15                4

您可以自行联接表。首先根据用户是否具有订阅类型"1"筛选用户,然后加入任何其他订阅类型。然后,检查用户是否有另一个订阅 (2,3,4(,该订阅在第一个订阅结束后开始。最后,您可以使用"摘要"按用户折叠,以查看我们的条件是否为真。

library(dplyr)
mydf%>%
filter(subscriptiontype==1)%>%
full_join(mydf[mydf$subscriptiontype!=1,], by="id", suffix=c(".Type1",".OtherType"))%>%
mutate(check=as.Date(startdate.OtherType)>=as.Date(stopdate.Type1))%>%
group_by(id)%>%
summarise(any(check, na.rm = TRUE))
id `any(check, na.rm = TRUE)`
<dbl> <lgl>                     
1     FALSE                     
2     TRUE                      
4     FALSE 

最新更新