Skip to content

Instantly share code, notes, and snippets.

@tjmahr
Created January 13, 2016 20:52
Show Gist options
  • Save tjmahr/46532a7d6f7903a2a8c2 to your computer and use it in GitHub Desktop.
Save tjmahr/46532a7d6f7903a2a8c2 to your computer and use it in GitHub Desktop.
days between lab visits
library("readr")
library("dplyr")
library("tidyr")
library("lubridate")
# Load the visit history, parse the timestamp as a Date
mdr <- read_csv("C:/Users/trist/Downloads/longitudinal_missing.csv") %>%
mutate(Date = mdy_hm(DateTime) %>% as.Date)
# for each study/subject, find difference between latest and earliest date
diffs <- mdr %>%
select(Study, Subj, Date) %>%
distinct %>%
group_by(Study, Subj) %>%
summarise(Diff = max(Date) - min(Date))
# Save the ones with more than 30 days between visits
diffs %>%
arrange(desc(Diff)) %>%
mutate(MoreThan30 = 30 < Diff) %>%
filter(MoreThan30) %>%
arrange(Study, Subj) %>%
select(Study, Subj, Diff) %>%
write_csv("C:/Users/trist/Downloads/days_betwen_visits.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment