- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个与此类似的数据框:
df<-read.csv(text="id;census;startDate;endDate
ZF001;died;16.10.2012;16.05.2015
ZF002;alive;20.10.2013
ZF003;alive;04.11.2013;
ZF004;died;11.11.2013;20.12.2014
ZF005;died;25.11.2013;16.06.2015
ZF006;alive;25.11.2014;
ZF007;survived;02.12.2014;19.01.2015
ZF008;alive;11.12.2014;
ZF009;survived;28.01.2015;12.03.2015", sep=";")
df$startDate<-as.Date(df$startDate, "%d.%m.%Y")
df$endDate<-as.Date(df$endDate, "%d.%m.%Y")
id year days
ZF001 2012 77
ZF001 2013 365
ZF001 2014 365
ZF001 2015 135
etc.
最佳答案
我假设您只希望对已故先证者使用此功能(因为活着的先证者没有结束日期),这是一个可能的 data.table
几乎不言自明的解决方案
library(data.table)
setDT(df)[census == "died",
as.data.table(table(year(seq.Date(startDate, endDate, by = "day")))),
by = id]
# id V1 N
# 1: ZF001 2012 77
# 2: ZF001 2013 365
# 3: ZF001 2014 365
# 4: ZF001 2015 136
# 5: ZF004 2013 51
# 6: ZF004 2014 354
# 7: ZF005 2013 37
# 8: ZF005 2014 365
# 9: ZF005 2015 167
id
,那么,我们使用的是
year
函数以提取年份,然后仅计算频率
dplyr
解决方案
library(dplyr)
df %>%
group_by(id) %>%
filter(census=='died') %>%
do(as.data.frame(table(year(seq.Date(.$startDate, .$endDate, by ='day')))))
Sys.Date
,在这种情况下我们可以定义一个简单的辅助函数
dateFunc <- function(x, y){
if(is.na(y)) {
as.data.table(table(year(seq.Date(x, Sys.Date(), by = "day"))))
} else as.data.table(table(year(seq.Date(x, y, by = "day"))))
}
setDT(df)[, setNames(dateFunc(startDate, endDate), c("Year", "Days")), by = id]
# id Year Days
# 1: ZF001 2012 77
# 2: ZF001 2013 365
# 3: ZF001 2014 365
# 4: ZF001 2015 136
# 5: ZF002 2013 73
# 6: ZF002 2014 365
# 7: ZF002 2015 222
# 8: ZF003 2013 58
# 9: ZF003 2014 365
# 10: ZF003 2015 222
# 11: ZF004 2013 51
# 12: ZF004 2014 354
# 13: ZF005 2013 37
# 14: ZF005 2014 365
# 15: ZF005 2015 167
# 16: ZF006 2014 37
# 17: ZF006 2015 222
# 18: ZF007 2014 30
# 19: ZF007 2015 19
# 20: ZF008 2014 21
# 21: ZF008 2015 222
# 22: ZF009 2015 44
df <- structure(list(id = structure(1:9, .Label = c("ZF001", "ZF002",
"ZF003", "ZF004", "ZF005", "ZF006", "ZF007", "ZF008", "ZF009"
), class = "factor"), census = structure(c(2L, 1L, 1L, 2L, 2L,
1L, 3L, 1L, 3L), .Label = c("alive", "died", "survived"), class = "factor"),
startDate = structure(c(15629, 15998, 16013, 16020, 16034,
16399, 16406, 16415, 16463), class = "Date"), endDate = structure(c(16571,
NA, NA, 16424, 16602, NA, 16454, NA, 16506), class = "Date")), .Names = c("id",
"census", "startDate", "endDate"), row.names = c(NA, -9L), class = "data.frame")
关于r - 从数据框计算每年的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31915790/
我的目标是获取购买给定产品所需的平均天数。如果 Product_A 在给定时间段内购买了 3 次(“2012-12-01”、“2012-12-05”、“2012-12-10”),那么我们的平均订单间隔
我在计算利息天数时有一个有趣的错误。我每天检查并检查今天是哪一天(1 到 31)。现在我发现了一个问题:10月计数不正常。这意味着 27 号是 26 号,或者 29 号是 28 号。这是一个众所周知的
你好我一直在做一些程序,我的程序是获取一年中过去的天数。现在,当我尝试运行时,输出结果为“4438232”。 例如,如果用户输入 (mm-dd-yy) 3-18-2013,则该年经过的扩孔天数为 77
我看到此问题已针对 Java 得到解答, JavaScript , 和 PHP ,但不是 C#。那么,如何在 C# 中计算两个日期之间的天数? 最佳答案 假设 StartDate 和 EndDate
QQ每天抽超级会员成长值、天数 非必中 手Q打开链接进入活动,下拉页面随便分享一下领取抽奖机会,亲测3点成长值! 活动地址:https://sourl.cn/37CZad 手Q扫码:
是否有任何内置函数可以找出时间戳之间的天数、添加天数或查找时间戳之间的月数?目前,我将日期作为字符串存储在我的文档中。 例如,如果我这样做: return (DATE_TIMESTAMP("2014-
我无法弄清楚如何为以下场景编写公式。我需要根据另一列计算日期,但需要根据原始列的星期几添加天数。 If day of week equals 'Mon/Wed/Fri/Sat' then add 5
我有两个系列的 Pandas 约会时间。我为数据框中的每一行减去它们,并添加一列以获取两个日期时间之间的时间增量。随后我想使用该时间增量来扩展另一个功能。所以我想对那个时间增量进行一些划分。没有骰子。
这个问题已经有答案了: "cannot find symbol: method" but the method is declared (3 个回答) How to add one day to a
我的代码是这样的,但第三行出错 mm/dd/yyyy 我的数据库日期格式如下: SELECT evep year(date='MM/dd/yyyy'), month(da
我有两个 jQuery UI 日期选择器,当它们都选择了日期时,我希望这些日期之间的差异显示在单独的 input[type="text"] 一旦选择了第二个日期。 此外,理想情况下,我希望我的计数减去
我有两列:rental_date 和 actual_retdate。我需要找到 actual_retdate 和 rental_date 之间的天数。 actual_retdate 在某些情况下可以为
运行 MySql 和 Yii 我需要更改当前查找当月记录的关系查询以查找过去 45 天的所有记录。 这是我当前的查询: 'itemCount' => array(self::STAT, 'It
我想创建一个新列,它将显示两个日期之间的时间增量(以天为单位),如以下 pandas 数据框所示: >>> hg[['not inc','date']] not inc
我有: 年份数字(可以是任何年份) 月份数(从一月到十二月) 周数(第 1、2、3、4、最后) 工作日(周日、周一、周二、周三、周四、周五、周六) 我需要获取天数 [从 1 到 ~31] - "YYY
我需要为一些项目倒数 90 天、120 天和 160 天。我将如何编码?我一直在寻找 java 代码,但是在我为 android 开发时会出现错误。 我需要用今天的日期减去 x 天,然后将结果显示在屏
我的要求是计算给定的两个日期之间的天数,不包括星期六和星期日。 示例: Start date - 10/09/15 and End date 18/09/15 Result: 7 日期采用 DD/MM
我想比较我的 Android 应用程序的两个日期,但我遇到了一个非常奇怪的问题。 例如: 如果我将 回到过去 日期设置为 127 天前: this.dateEvent = System.current
如何返回所提供日期的 CumSum 天数? import pandas as pd df = pd.DataFrame({ 'date': ['2019-01-01','2019-01-03'
例如,我可以使用以下代码获取当前日期以显示在 DatePicker 对话框中。 final Calendar c = Calendar.getInstance(); mYear = c
我是一名优秀的程序员,十分优秀!