r - Comparing dates inside group using same reference -
i have data table different patients ("spell") , several temperature ("temp") measures each patient ("episode"). have date , time in each temperature taken.
spell episode date temp 1 3 2-1-17 21:00 40 1 2 2-1-17 20:00 36 1 1 1-1-17 10:00 37 2 3 2-1-17 15:00 36 2 2 2-1-17 10:00 37 2 1 1-1-17 8:00 36 3 1 3-1-17 10:00 40 4 3 4-1-17 15:00 36 4 2 3-1-17 12:00 40 4 1 3-1-17 10:00 39 5 7 3-1-17 17:30 36 5 6 2-1-17 17:00 36 5 5 2-1-17 16:00 37 5 1 1-1-17 9:00 36 5 4 1-1-17 14:00 39 5 3 1-1-17 13:00 40 5 2 1-1-17 11:00 39
i interested in keeping measurements done 24h prior last one, have grouped observations spell , reverse date, unsure on how in-group comparison using same reference (in case, first row each group). result should be:
spell episode date temp 1 3 2-1-17 21:00 40 1 2 2-1-17 20:00 36 2 3 2-1-17 15:00 36 2 2 2-1-17 10:00 37 3 1 3-1-17 10:00 40 4 3 4-1-17 15:00 36 5 7 3-1-17 17:30 36
would appreciate ideas point me right direction.
edit: date in d-m-yy h:m format. here's dput data:
structure(list(spell = c(1l, 1l, 1l, 2l, 2l, 2l, 3l, 4l, 4l, 4l, 5l, 5l, 5l, 5l, 5l, 5l, 5l), episode = c(3l, 2l, 1l, 3l, 2l, 1l, 1l, 3l, 2l, 1l, 7l, 6l, 5l, 1l, 4l, 3l, 2l), date = c("2-1-17 21:00", "2-1-17 20:00", "1-1-17 10:00", "2-1-17 15:00", "2-1-17 10:00", "1-1-17 8:00", "3-1-17 10:00", "4-1-17 15:00", "3-1-17 12:00", "3-1-17 10:00", "3-1-17 17:30", "2-1-17 17:00", "2-1-17 16:00", "1-1-17 9:00", "1-1-17 14:00", "1-1-17 13:00", "1-1-17 11:00" ), temp = c(40l, 36l, 37l, 36l, 37l, 36l, 40l, 36l, 40l, 39l, 36l, 36l, 37l, 36l, 39l, 40l, 39l)), .names = c("spell", "episode", "date", "temp"), class = c("data.table", "data.frame"), row.names = c(na, -17l), .internal.selfref = <pointer: 0x00000000001f0788>)
library(dplyr) df %>% mutate(date2 = as.numeric(strptime(df$date, "%d-%m-%y %h:%m"))) %>% group_by(spell) %>% filter(date2 >= (max(date2) - 60*60*24)) %>% select(-date2)
Comments
Post a Comment