Closed andkov closed 8 years ago
For the sake of demonstration, I changed the last dementia point for subject 5.
library(magrittr)
ds_alive <- structure(list(X = 1:16, id = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), fu_year = c(0L, 1L, 2L, 3L,
4L, 5L, 6L, 0L, 1L, 0L, 1L, 2L, 3L, 4L, 5L, 6L), age_bl = c(78.53798768,
78.53798768, 78.53798768, 78.53798768, 78.53798768, 78.53798768,
78.53798768, 83.62765229, 83.62765229, 78.65571526, 78.65571526,
78.65571526, 78.65571526, 78.65571526, 78.65571526, 78.65571526
), age_death = c(85.26214921, 85.26214921, 85.26214921, 85.26214921,
85.26214921, 85.26214921, 85.26214921, NA, NA, 85.43189596, 85.43189596,
85.43189596, 85.43189596, 85.43189596, 85.43189596, 85.43189596
), died = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), age_at_visit = c(78.53798768, 79.53730322, 80.54757016,
81.54414784, 82.54346338, 83.57837098, 84.57221081, 83.62765229,
85.36892539, 78.65571526, 79.68788501, 80.64339493, 81.67830253,
82.68309377, 83.64134155, 84.67077344), dementia = c(0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L)), .Names = c("X",
"id", "fu_point", "age_bl", "age_death", "dead_currently", "age_at_visit",
"dementia"), class = "data.frame", row.names = c(NA, -16L)
)
ds_alive <- ds_alive %>%
dplyr::select(-X) %>%
dplyr::rename(dementia_now=dementia) %>%
dplyr::group_by(id) %>%
dplyr::mutate(
dead_currently = as.logical(dead_currently),
dementia_now = as.logical(dementia_now),
dementia_ever = any(dementia_now)
) %>%
dplyr::ungroup()
ds_alive
ds_dead <- ds_alive %>%
dplyr::filter(!is.na(age_death)) %>%
dplyr::group_by(id) %>%
dplyr::arrange(fu_point) %>%
dplyr::summarize(
fu_point = max(fu_point) + 1L,
age_bl = max(age_bl), #Assumes same for all points within a person
age_death = max(age_death),
dead_currently = TRUE,
age_at_visit = NA_real_,
dementia_last = dplyr::last(dementia_now),
dementia_now = ifelse(dementia_last, TRUE, NA),
dementia_ever = any(dementia_now)
) %>%
dplyr::ungroup() %>%
dplyr::select(-dementia_last)
ds_dead
ds <- ds_alive %>%
dplyr::union(ds_dead) %>%
dplyr::arrange(id, fu_point)
ds
> ds
Source: local data frame [18 x 8]
id fu_point age_bl age_death dead_currently age_at_visit dementia_now dementia_ever
(int) (int) (dbl) (dbl) (lgl) (dbl) (lgl) (lgl)
1 3 0 78.53799 85.26215 TRUE 78.53799 FALSE FALSE
2 3 1 78.53799 85.26215 TRUE 79.53730 FALSE FALSE
3 3 2 78.53799 85.26215 TRUE 80.54757 FALSE FALSE
4 3 3 78.53799 85.26215 TRUE 81.54415 FALSE FALSE
5 3 4 78.53799 85.26215 TRUE 82.54346 FALSE FALSE
6 3 5 78.53799 85.26215 TRUE 83.57837 FALSE FALSE
7 3 6 78.53799 85.26215 TRUE 84.57221 FALSE FALSE
8 3 7 78.53799 85.26215 TRUE NA NA NA
9 4 0 83.62765 NA FALSE 83.62765 FALSE FALSE
10 4 1 83.62765 NA FALSE 85.36893 FALSE FALSE
11 5 0 78.65572 85.43190 TRUE 78.65572 FALSE TRUE
12 5 1 78.65572 85.43190 TRUE 79.68789 FALSE TRUE
13 5 2 78.65572 85.43190 TRUE 80.64339 TRUE TRUE
14 5 3 78.65572 85.43190 TRUE 81.67830 FALSE TRUE
15 5 4 78.65572 85.43190 TRUE 82.68309 FALSE TRUE
16 5 5 78.65572 85.43190 TRUE 83.64134 FALSE TRUE
17 5 6 78.65572 85.43190 TRUE 84.67077 TRUE TRUE
18 5 7 78.65572 85.43190 TRUE NA TRUE TRUE
Exposition
For the data of the shape
Write a function to create a variable
age
which would combineage_at_visit
andage_death
. This would add an additional row for each uniqueid
. The ultimate purpose is to bring the data to the shapewhere
state==1
indicates that the person alive and healthy(dementia==0
),state==2
indicates that the person is alive but has a condition (dementia==1
), andstate==3
is a dead state. (see details in ELECT vignette, p.3)Starter code