我有以下有关患者某种症状(“症状”)发生的数据。我必须计算此症状发生的发作次数(“ symptom_episode”),如果该症状在超过48小时内不存在,则需要增加“ recovery” +1。 这是一个示例,其中包含“ symptom_episode”和“ recovery”的所需输出:
dat=data.table(
date=c("2015-01-01 06:00:00 UTC","2015-01-01 16:53:00 UTC","2015-01-02 05:15:00 UTC","2015-01-03 05:28:00 UTC","2015-01-04 05:13:00 UTC","2015-01-05 05:25:00 UTC","2015-01-06 05:11:00 UTC","2015-01-07 05:25:00 UTC","2015-01-08 05:20:00 UTC","2015-01-09 05:17:00 UTC","2015-01-09 15:25:00 UTC","2015-01-10 05:22:00 UTC","2015-01-11 05:19:00 UTC"),
symptom=c(0,1,1,1,0,0,1,1,0,0,0,0,0),
symptom_episode=c(0,1,1,1,0,0,2,2,0,0,0,0,0),
recovery=c(0,0,0,0,0,0,0,0,1,1,1,1,1)
)
我的问题是,我不知道如何计算症状发作的数量,也不知道如何进行恢复部分。 我相信使用某种rollapply解决方案应该可以实现恢复部分!?
我将非常感谢您的帮助或建议!
答案 0 :(得分:0)
尝试一下。我更改了最后一个值以测试结果(应为1,因为symptom_episode = 0且时间为<48小时。
> library(data.table)
> library(dplyr)
> dat=data.table(
+ date=c("2015-01-01 06:00:00 UTC","2015-01-01 16:53:00 UTC","2015-01-02 05:15:00 UTC","2015-01-03 05:28:00 UTC","2015-01-04 05:13:00 UTC","2015-01-05 05:25:00 UTC","2015-01-06 05:11:00 UTC","2015-01-07 05:25:00 UTC","2015-01-08 05:20:00 UTC","2015-01-09 05:17:00 UTC","2015-01-09 15:25:00 UTC","2015-01-10 05:22:00 UTC","2015-01-15 05:19:00 UTC"),
+ symptom=c(0,1,1,1,0,0,1,1,0,0,0,0,0),
+ symptom_episode=c(0,1,1,1,0,0,2,2,0,0,0,0,0),
+ recovery=c(0,0,0,0,0,0,0,0,0,0,0,0,0)
+ )
>
>
> dat$hours <- c(NA, difftime(dat$date[-1],
+ dat$date[-nrow(dat)],
+ units="hours"))
>
> dat$recovery[dat$hours<48] <- 0
>
> dat$recovery[dat$hours>48 & dat$symptom_episode==0]<-1
>
> head(dat)
date symptom symptom_episode recovery hours
1: 2015-01-01 06:00:00 UTC 0 0 0 NA
2: 2015-01-01 16:53:00 UTC 1 1 0 10.88333
3: 2015-01-02 05:15:00 UTC 1 1 0 12.36667
4: 2015-01-03 05:28:00 UTC 1 1 0 24.21667
5: 2015-01-04 05:13:00 UTC 0 0 0 23.75000
6: 2015-01-05 05:25:00 UTC 0 0 0 24.20000
...
head(dat[13,]
#correctly updates 'recovery' value to 1.
date symptom symptom_episode recovery hours
1: 2015-01-15 05:19:00 UTC 0 0 1 119.95
答案 1 :(得分:0)
这是一种- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
{
if (((self.frame.origin.x + self.frame.size.width) > screen.frame.size.width) && (screen.frame.size.width > 0))
{
frameRect.origin.x = (screen.frame.size.width - self.frame.size.width);
}
if (self.frame.origin.y < 0)
{
frameRect.origin.y = 0;
}
return frameRect;
}
的方法,虽然不是最简洁的方法,但还是有道理的。
dplyr