如何根据两个日期列在一年内计算persondays
我想计算一个ID/行(人)在一年内拥有的天数。下面是我的数据集的示例。 (Inschrijfdatum = 注册日期,Uitschrijfdatum = 注销日期)
Inschrijfdatum Uitschrijfdatum
<date> <date>
1 1996-02-22 2019-01-11
2 2011-10-31 2019-02-25
3 1992-06-15 2019-03-10
4 2001-11-13 2022-01-01
5 2019-02-18 2019-09-07
6 2019-12-30 2022-01-01
# ... with 12 more rows
df <-structure(list(Inschrijfdatum = structure(c(9548, 15278, 8201,
11639, 10029, 15134, 17756, 16526, 17945, 13427, 7348, 16072,
13195, 8231, 12620, 14767, 17893, 18054, 15300, 12006, 15399,
10092), class = "Date"), Uitschrijfdatum = structure(c(17907,
17952, 17965, 17981, 18001, 18079, 18102, 18137, 18146, 18190,
18198, 18199, 18222, 18233, 18240, 18487, 18496, 18542, 18555,
18581, 18670, 18993), class = "Date")), row.names = c(NA, -22L
), class = c("tbl_df", "tbl", "data.frame"))
我想创建一个新列,其中包含特定年份(在本例中为 2019 年)的总“人”天数
Inschrijfdatum Uitschrijfdatum persondays
<date> <date>
1 1996-02-22 2019-01-11 11
2 2011-10-31 2019-02-25 56
3 1992-06-15 2019-03-10 69
4 2001-11-13 2022-01-01 365
5 2019-02-18 2019-09-07 200
6 2019-12-30 2022-01-01 1
我尝试按步骤执行此操作:
- df $persondays <- df$Uitschrijfdatum - as.Date("2019-01-01", format= "%Y-%m-%d"))
- df$persondays <- as.Date("2020-01-01", format= "%Y-%m-%d")) - df$Inschrijfdatum
此课程不太正常工作。我想创建一个脚本,可以对以下每种可能性进行计数,
2019 2020 1=inschrijfdatum, 2= uitschrijfdatum
|--------------------------|
1-------------2 ? (2 - as.date(2019))
1---------------------------------------2 ? maximum of 365 days
1-----------2 ? days between 1 and 2
1---------2 ? (as.date(2020) - 1)
我不太明白这一点。任何帮助将不胜感激!
非常感谢!
I would like to count the amount of days an ID/row(person) has during a period of 1 year. Below an example of my dataset. (Inschrijfdatum = registration date, Uitschrijfdatum = deregistration date)
Inschrijfdatum Uitschrijfdatum
<date> <date>
1 1996-02-22 2019-01-11
2 2011-10-31 2019-02-25
3 1992-06-15 2019-03-10
4 2001-11-13 2022-01-01
5 2019-02-18 2019-09-07
6 2019-12-30 2022-01-01
# ... with 12 more rows
df <-structure(list(Inschrijfdatum = structure(c(9548, 15278, 8201,
11639, 10029, 15134, 17756, 16526, 17945, 13427, 7348, 16072,
13195, 8231, 12620, 14767, 17893, 18054, 15300, 12006, 15399,
10092), class = "Date"), Uitschrijfdatum = structure(c(17907,
17952, 17965, 17981, 18001, 18079, 18102, 18137, 18146, 18190,
18198, 18199, 18222, 18233, 18240, 18487, 18496, 18542, 18555,
18581, 18670, 18993), class = "Date")), row.names = c(NA, -22L
), class = c("tbl_df", "tbl", "data.frame"))
I would like to create a new column in which the total 'person'days are included in a certain year (in this case year 2019)
Inschrijfdatum Uitschrijfdatum persondays
<date> <date>
1 1996-02-22 2019-01-11 11
2 2011-10-31 2019-02-25 56
3 1992-06-15 2019-03-10 69
4 2001-11-13 2022-01-01 365
5 2019-02-18 2019-09-07 200
6 2019-12-30 2022-01-01 1
I've tried doing it in steps:
- df$persondays <- df$Uitschrijfdatum - as.Date("2019-01-01", format= "%Y-%m-%d"))
- df$persondays <- as.Date("2020-01-01", format= "%Y-%m-%d")) - df$Inschrijfdatum
This offcourse doesnt work quite right. I would like to create a script that can count it for each of the following possibilities
2019 2020 1=inschrijfdatum, 2= uitschrijfdatum
|--------------------------|
1-------------2 ? (2 - as.date(2019))
1---------------------------------------2 ? maximum of 365 days
1-----------2 ? days between 1 and 2
1---------2 ? (as.date(2020) - 1)
I cant quite figure this out. Any help would be much appreciated!
Many thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许就像这样?
结果
Perhaps like so?
Result