Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. $ admitDate : Date, format: "2009-09-19" "2010-01-24" "2010-09-30" ...
  2. $ dcDate : Date, format: "2009-09-23" "2010-01-27" "2010-10-04" ...
  3. $ los : num 4 3 4 25 6 3 6 2 2 3 ...
  4.  
  5. > dput(head(df,20))
  6. structure(list(Unit.Number = c(2013459L, 2013459L, 2047815L,
  7. 1362858L, 1331174L, 2068040L, 1363711L, 2175972L, 2036695L, 1426614L,
  8. 1403126L, 2083126L, 1334063L, 1349385L, 1404482L, 2175545L, 1296600L,
  9. 1293220L, 1336768L, 2148401L), admitDate = structure(c(14506,
  10. 14633, 14882, 15172, 14945, 15632, 15482, 15601, 16096, 15843,
  11. 16013, 15548, 15436, 15605, 16115, 15597, 15111, 15050, 15500,
  12. 15896), class = "Date"), dcDate = structure(c(14510, 14636, 14886,
  13. 15197, 14951, 15635, 15488, 15603, 16098, 15846, 16016, 15552,
  14. 15438, 15606, 16118, 15598, 15113, 15058, 15501, 15915), class = "Date"),
  15. los = c(4, 3, 4, 25, 6, 3, 6, 2, 2, 3, 3, 4, 2, 1, 3, 1,
  16. 2, 8, 1, 19)), .Names = c("Unit.Number", "admitDate", "dcDate",
  17. "los"), row.names = c(NA, 20L), class = "data.frame")
  18.  
  19. days <- seq(min(df$admitDate), max(df$dcDate), "day")
  20. no.patients <- data.frame(
  21. Date = days,
  22. Num = sapply(days, function(d) sum(d >= df$admitDate & d <= df$dcDate)),
  23. Patients = sapply(days, function(d)
  24. toString(df$Unit.Number[d >= df$admitDate & d <= df$dcDate]))
  25. )
  26.  
  27. > days <- seq(min(df$admitDate), max(df$dcDate), "day")
  28. Error in seq.int(0, to0 - from, by) : 'to' cannot be NA, NaN or infinite
  29. > no.patients <- data.frame(Date = d,
  30. + Num = sapply(days, function(d) sum(d >= df$admitDate & d <= df$dcDate)))
  31. Error in data.frame(Date = d, Num = sapply(days, function(d) sum(d >= :
  32. object 'd' not found
  33.  
  34. > df <- df[rowSums(is.na(df)) < 0, ]
  35.  
  36. > days <- seq(min(df$admitDate), max(df$dcDate), "day")
  37. Error in seq.int(0, to0 - from, by) : 'to' cannot be NA, NaN or infinite
  38. In addition: Warning messages:
  39. 1: In min.default(numeric(0), na.rm = FALSE) :
  40. no non-missing arguments to min; returning Inf
  41. 2: In max.default(numeric(0), na.rm = FALSE) :
  42. no non-missing arguments to max; returning -Inf
  43. > no.patients <- data.frame(Date = d,
  44. + Num = sapply(days, function(d) sum(d >= df$admitDate & d <= df$dcDate)))
  45. Error in data.frame(Date = d, Num = sapply(days, function(d) sum(d >= :
  46. object 'd' not found
  47.  
  48. df <- structure(list(Unit.Number = c(2013459L, 2013459L, 2047815L,
  49. 1362858L, 1331174L, 2068040L, 1363711L, 2175972L, 2036695L, 1426614L,
  50. 1403126L, 2083126L, 1334063L, 1349385L, 1404482L, 2175545L, 1296600L,
  51. 1293220L, 1336768L, 2148401L), admitDate = structure(c(14506,
  52. 14633, 14882, 15172, 14945, 15632, 15482, 15601, 16096, 15843,
  53. 16013, 15548, 15436, 15605, 16115, 15597, 15111, 15050, 15500,
  54. 15896), class = "Date"), dcDate = structure(c(14510, 14636, 14886,
  55. 15197, 14951, 15635, 15488, 15603, 16098, 15846, 16016, 15552,
  56. 15438, 15606, 16118, 15598, 15113, 15058, 15501, 15915), class = "Date"),
  57. los = c(4, 3, 4, 25, 6, 3, 6, 2, 2, 3, 3, 4, 2, 1, 3, 1,
  58. 2, 8, 1, 19)), .Names = c("Unit.Number", "admitDate", "dcDate",
  59. "los"), row.names = c(NA, 20L), class = "data.frame")
  60.  
  61. # create dataframe for computing the size of the queue (concurrent patients)
  62. x <- data.frame(date = c(df$admitDate, df$dcDate)
  63. , op = c(rep(1, nrow(df)), rep(-1, nrow(df)))
  64. , Unit.Number = c(df$Unit.Number, df$Unit.Number)
  65. )
  66. # sort and calculate concurrent patients
  67. x <- x[order(x$date), ] # sort in time order
  68. x$cum <- cumsum(x$op)
  69.  
  70. # 'x' will have the 'cum' equal to the number of patients concurrently.
  71. # for 'op' == 1, you have the patient ID and 'cum' will be the number of
  72. # patients at that time.
  73.  
  74. plot(x$date, x$cum, type = 's')
  75.  
  76. > head(x,10)
  77. date op Unit.Number cum
  78. 1 2009-09-19 1 2013459 1
  79. 21 2009-09-23 -1 2013459 0
  80. 2 2010-01-24 1 2013459 1
  81. 22 2010-01-27 -1 2013459 0
  82. 3 2010-09-30 1 2047815 1
  83. 23 2010-10-04 -1 2047815 0
  84. 5 2010-12-02 1 1331174 1
  85. 25 2010-12-08 -1 1331174 0
  86. 18 2011-03-17 1 1293220 1
  87. 38 2011-03-25 -1 1293220 0
  88. >
  89.  
  90. days <- seq(min(df$admitDate), max(df$dcDate), "day")
  91. no.patients <- data.frame(
  92. Date = days,
  93. Num = sapply(days, function(d) sum(d >= df$admitDate & d <= df$dcDate)),
  94. Patients = sapply(days, function(d)
  95. toString(df$Unit.Number[d >= df$admitDate & d <= df$dcDate]))
  96. )
  97.  
  98. > head(no.patients)
  99. Date Num Patients
  100. 1 2009-09-19 1 2013459
  101. 2 2009-09-20 1 2013459
  102. 3 2009-09-21 1 2013459
  103. 4 2009-09-22 1 2013459
  104. 5 2009-09-23 1 2013459
  105. 6 2009-09-24 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement