gpt4 book ai didi

r - 计算自R中的最后一个事件以来的天数

转载 作者:行者123 更新时间:2023-12-03 14:12:42 25 4
gpt4 key购买 nike

我的问题涉及到如何计算自R事件最后一次发生以来的天数。
以下是数据的最小示例:

df <- data.frame(date=as.Date(c("06/07/2000","15/09/2000","15/10/2000","03/01/2001","17/03/2001","23/05/2001","26/08/2001"), "%d/%m/%Y"), 
event=c(0,0,1,0,1,1,0))
date event
1 2000-07-06 0
2 2000-09-15 0
3 2000-10-15 1
4 2001-01-03 0
5 2001-03-17 1
6 2001-05-23 1
7 2001-08-26 0


二进制变量(事件)的值1表示事件已发生,否则值为0。重复观察是在不同时间进行的( date
预期输出如下,以及自上次事件( tae)以来的天数:

 date        event       tae
1 2000-07-06 0 NA
2 2000-09-15 0 NA
3 2000-10-15 1 0
4 2001-01-03 0 80
5 2001-03-17 1 153
6 2001-05-23 1 67
7 2001-08-26 0 95


我到处寻找类似问题的答案,但是它们并不能解决我的特定问题。我试图实施
来自相似帖子( Calculate elapsed time since last event)的以下是最接近的我
解决方案:

library(dplyr)
df %>%
mutate(tmp_a = c(0, diff(date)) * !event,
tae = cumsum(tmp_a))


产生的输出如下所示,与预期的不太一样:

        date event tmp_a tae
1 2000-07-06 0 0 0
2 2000-09-15 0 71 71
3 2000-10-15 1 0 71
4 2001-01-03 0 80 151
5 2001-03-17 1 0 151
6 2001-05-23 1 0 151
7 2001-08-26 0 95 246


对于如何微调此方法或其他方法的任何帮助,将不胜感激。

最佳答案

您可以尝试这样的事情:

# make an index of the latest events
last_event_index <- cumsum(df$event) + 1

# shift it by one to the right
last_event_index <- c(1, last_event_index[1:length(last_event_index) - 1])

# get the dates of the events and index the vector with the last_event_index,
# added an NA as the first date because there was no event
last_event_date <- c(as.Date(NA), df[which(df$event==1), "date"])[last_event_index]

# substract the event's date with the date of the last event
df$tae <- df$date - last_event_date
df

# date event tae
#1 2000-07-06 0 NA days
#2 2000-09-15 0 NA days
#3 2000-10-15 1 NA days
#4 2001-01-03 0 80 days
#5 2001-03-17 1 153 days
#6 2001-05-23 1 67 days
#7 2001-08-26 0 95 days

关于r - 计算自R中的最后一个事件以来的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30391333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com