大家好我有这个问题,我想计算日期之间的天数,前提是:
状态A为基准日期,所有计算都必须以该日期为参考(按ID分组)
我必须将状态B,C,D的日期定为较早
我必须计算天数并在不同的列中显示
前任。
用R生成表
ColID = c(1, 1, 1, 1, 1, 2, 2, 2)
ColStatus = c("A", "B", "B", "C", "D", "A", "C", "C")
ColDate = c("01/01/2018","02/03/2018", "05/04/2018", "04/05/2018", "04/05/2018", "02/01/2018", "04/03/2018", "05/04/2018")
data.frame(ColID, ColStatus, ColDate)
我如何进行计算
For ColID = 1
Status A = 01/01/2018
Status B (I Have to select the older one) = 02/03/2018
Status C = 04/05/2018
Status D = 04/05/2018
ResultColB = 02/03/2018 - 01/01/2018 = 60
ResultColC = 04/05/2018 - 01/01/2018 = 123
ResultColD = 04/05/2018 - 01/01/2018 = 123
结果表(天)
用R生成表
ColID = c(1,2)
ResultColStatusB = c(60,0)
ResultColStatusC = c(123,61)
data.frame(ColID, ResultColStatusB, ResultColStatusC, ResultColStatusB)
该问题可以用R,Python或SQL解决,请问有什么推荐给我解决该问题的呢?
相关分类