Created
May 24, 2016 05:52
-
-
Save njtierney/54756cacb308a1a49ee25126da2723eb to your computer and use it in GitHub Desktop.
smoking data: Has there been a smoker in the past
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# calculate if there has been a smoker in the past | |
# "smoked_previously" | |
data_prac <- data_frame(ID = c(1, 1, 1, 1, | |
201, 201, | |
342, 342, 342, | |
613, 613, 613, 613, 613), | |
time = c(1, 2, 3, 4, | |
1, 2, | |
1, 2, 3, | |
1, 2, 3, 4, 5), | |
smoke = c("smoker", "smoker", "non_smoker", "non_smoker", | |
"non_smoker", "smoker", | |
"non_smoker", "non_smoker", "non_smoker", | |
"non_smoker", "non_smoker", "smoker", "non_smoker", "non_smoker")) | |
#> data_prac | |
#> Source: local data frame [14 x 3] | |
#> | |
#> ID time smoke | |
#> <dbl> <dbl> <chr> | |
#> 1 1 1 smoker | |
#> 2 1 2 smoker | |
#> 3 1 3 non_smoker | |
#> 4 1 4 non_smoker | |
#> 5 201 1 non_smoker | |
#> 6 201 2 smoker | |
#> 7 342 1 non_smoker | |
#> 8 342 2 non_smoker | |
#> 9 342 3 non_smoker | |
#> 10 613 1 non_smoker | |
#> 11 613 2 non_smoker | |
#> 12 613 3 smoker | |
#> 13 613 4 non_smoker | |
#> 14 613 5 non_smoker | |
data_prac %>% | |
group_by(ID) %>% | |
arrange(time) %>% | |
mutate(smoke_hist = ifelse(cumsum(smoke == "smoker") >= 1, | |
yes = "has_smoked", | |
no = "non_smoker")) | |
#> Source: local data frame [14 x 4] | |
#> Groups: ID [4] | |
#> | |
#> ID time smoke smoke_hist | |
#> (dbl) (dbl) (chr) (chr) | |
#> 1 1 1 smoker has_smoked | |
#> 2 1 2 smoker has_smoked | |
#> 3 1 3 non_smoker has_smoked | |
#> 4 1 4 non_smoker has_smoked | |
#> 5 201 1 non_smoker non_smoker | |
#> 6 201 2 smoker has_smoked | |
#> 7 342 1 non_smoker non_smoker | |
#> 8 342 2 non_smoker non_smoker | |
#> 9 342 3 non_smoker non_smoker | |
#> 10 613 1 non_smoker non_smoker | |
#> 11 613 2 non_smoker non_smoker | |
#> 12 613 3 smoker has_smoked | |
#> 13 613 4 non_smoker has_smoked | |
#> 14 613 5 non_smoker has_smoked |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment