如何用R表示不等数量的时间序列数据

时间:2015-04-10 22:21:55

标签: r time-series

我有一个大型数据框(86000行),由几名患者组成,每次患者在住院期间都进行了多次血液检查(仅有3次检查:T1,T2和T3)。其中一些患者住院3天,有些住院168天。

这只是count function输出的一小部分,显示了在医院度过的日子差异很大:

No  Id     Days
148 29757  111
149 30368   36
150 31062   29
151 31993   24
152 32198   51
153 32438    6
154 32836   74
155 32944   24
156 33467   39
157 36108   90
158 36849    6
159 37136    3

我使用聚合来计算手段等,但我希望看到谁在住宿期间改善或恶化的摘要。

我认为这将涉及至少提取第一次和最后一次测试,并采取差异(越低越好)。但我找不到办法做到这一点。

我认为更简单的解决方案是将整个结果转换为有序数据(根据测试的正常范围),并查看其中有多少具有异常低或高值。不幸的是,几乎每个病人都有低潮和高潮。

理想情况下,我希望随着时间的推移,看到几位患者(或一组患者)的进展。但由于他们在不同的时间范围内住院,(过度简化)的结果就是这样:

Results of just 2 patients, who were hospitalised at entirely different time-frames

正如您所看到的,第一位患者(红点)以平庸值开始,迅速恶化(高值),然后改善(较低值)。第二名患者的进展尚不清楚,因为他/她的住院时间可能很短。

有人可以提出启动(代码或想法)吗?我检查了some questions关于multiple time-series plots with unequal observations的问题,但对我的情况没有帮助。 这里有一个示例(匿名)数据集:

structure(list(Id = c("10200", "10200", "10200", "10200", "10200", 
"10200", "10700", "10700", "10700", "10700", "10700", "10700", 
"10700", "10700", "10700", "10700", "10700", "10700", "10700", 
"10700", "10700", "10766", "10766", "10766", "10766", "10766", 
"10766", "10766", "10766", "10766", "10766", "10766", "10766", 
"10766", "10766", "10766", "10766", "10766", "10766", "10766"
), Date = structure(c(15068, 15068, 15068, 15069, 15069, 15069, 
15072, 15072, 15072, 15072, 15072, 15072, 15073, 15073, 15073, 
15075, 15075, 15075, 15078, 15078, 15078, 15073, 15074, 15074, 
15075, 15075, 15075, 15075, 15076, 15076, 15076, 15078, 15078, 
15078, 15081, 15082, 15083, 15084, 15085, 15085), class = "Date"), 
    Test = c("T1", "T2", "T3", "T1", "T2", "T3", "T1", "T1", 
    "T2", "T2", "T3", "T3", "T1", "T2", "T3", "T1", "T2", "T3", 
    "T1", "T2", "T3", "T1", "T1", "T2", "T1", "T1", "T2", "T2", 
    "T1", "T2", "T3", "T1", "T2", "T3", "T1", "T1", "T2", "T1", 
    "T1", "T2"), Result = c(131, 4.53, 5.4, 108, 3.19, 3.7, 125, 
    NA, 1.26, NA, NA, 3.8, 125, 0.97, 4.2, 73, 0.84, 6.6, 48, 
    0.52, 4.8, 60, 75, 0.83, 52, 51, 0.62, 0.65, 40, 0.57, 4.1, 
    45, 0.54, 3.7, 96, 77, 1.04, 134, 144, 0.95)), .Names = c("Id", 
"Date", "Test", "Result"), row.names = c(3L, 6L, 4L, 2L, 1L, 
5L, 10L, 14L, 9L, 19L, 8L, 11L, 20L, 18L, 7L, 17L, 13L, 21L, 
12L, 15L, 16L, 22L, 28L, 29L, 24L, 31L, 26L, 33L, 34L, 32L, 37L, 
23L, 35L, 25L, 38L, 36L, 30L, 27L, 39L, 40L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

我不知道这是否是您想要的,但您可以使用dplyr包。下面的代码将按“Id”对数据进行分组,然后找到第一个&结果中的最后一个值,最后计算新列中的“差异”

mydata <- structure(list(Id=c ( "10200", "10200", "10200", "10200", "10200", "10200", "10700", "10700", "10700", "10700", "10700", "10700", "10700", "10700", "10700", "10700", "10700", "10700", "10700", "10700", "10700", "10766", "10766", "10766",
"10766", "10766", "10766", "10766", "10766", "10766", "10766", "10766", "10766", "10766", "10766", "10766", "10766", "10766", "10766", "10766" ), Date=s tructure(c(15068, 15068, 15068, 15069, 15069, 15069, 15072, 15072, 15072, 15072, 15072, 15072, 15073, 15073,
15073, 15075, 15075, 15075, 15078, 15078, 15078, 15073, 15074, 15074, 15075, 15075, 15075, 15075, 15076, 15076, 15076, 15078, 15078, 15078, 15081, 15082, 15083, 15084, 15085, 15085), class="Date" ), Test=c ( "T1", "T2", "T3", "T1", "T2", "T3", "T1",
"T1", "T2", "T2", "T3", "T3", "T1", "T2", "T3", "T1", "T2", "T3", "T1", "T2", "T3", "T1", "T1", "T2", "T1", "T1", "T2", "T2", "T1", "T2", "T3", "T1", "T2", "T3", "T1", "T1", "T2", "T1", "T1", "T2"), Result=c (131, 4.53, 5.4, 108, 3.19, 3.7, 125, NA, 1.26,
NA, NA, 3.8, 125, 0.97, 4.2, 73, 0.84, 6.6, 48, 0.52, 4.8, 60, 75, 0.83, 52, 51, 0.62, 0.65, 40, 0.57, 4.1, 45, 0.54, 3.7, 96, 77, 1.04, 134, 144, 0.95)), .Names=c ( "Id", "Date", "Test", "Result"), row.names=c (3L, 6L, 4L, 2L, 1L, 5L, 10L, 14L, 9L, 19L,
8L, 11L, 20L, 18L, 7L, 17L, 13L, 21L, 12L, 15L, 16L, 22L, 28L, 29L, 24L, 31L, 26L, 33L, 34L, 32L, 37L, 23L, 35L, 25L, 38L, 36L, 30L, 27L, 39L, 40L), class="data.frame" )

library(dplyr) 

result <- mydata %>%
  group_by(Id) %>%  
  summarise_each(funs(first, last), Result) %>%
  mutate(difference = first - last)
result