IALSA / ialsa-2016-amsterdam

Multi-study and multivariate evaluation of healthy life expectancy (HLE): An IALSA workshop on multistate modeling using R
GNU General Public License v2.0
0 stars 0 forks source link

Data Grooming. Session II. 2016-03-31-07:00 PDT #9

Open andkov opened 8 years ago

andkov commented 8 years ago

Agenda

andkov commented 8 years ago

Objective

We need to de construct the names of the melted variables into individual components, which would create values for the new variables. Consider the data:

     variable
1   intage2000
2   intage2005
3   intage2009
4  dressing.70
5  dressing.75
6  dressing.79
7      feed.70
8      feed.75
9      feed.79
10     dem2000
11     dem2005
12     dem2009
13        AE69
14        AG58
15       AH108
16       AE320
17       AG384
18       AH414

Solution

ds %>% dplyr::arrange(variable)

regex <- "^(\\w+?)\\.(\\d{2})$" 
regex2 <- "^(AE|AG|AH)(\\d+?)$"
regex3 <- "^(\\w+?)(\\d{4})$"
d <- ds %>% dplyr::mutate(
  age = variable, # copy existing
  age = gsub(regex, "\\2", variable),
  age = gsub(regex2, "\\1", age),
  age = gsub(regex3, "\\2", age),
  age = car::recode(age, " c('2000','AE')='70';
                    c('2005','AG')='75';
                    c('2009','AH')='79';"),
  measure = gsub(regex, "\\1", variable), 
  measure = gsub(regex2, "\\2", measure),
  measure = gsub(regex3, "\\1", measure),
  measure = car::recode(measure, " c('320','384','414')='smoking';
                                   c('69','58','108')='bmi' "), 
                          year = age + 1930
  ) %>% dplyr::arrange(desc(variable))
  (d) 

      variable age  measure year
1   intage2009  79   intage 2009
2   intage2005  75   intage 2005
3   intage2000  70   intage 2000
4      feed.79  79     feed 2009
5      feed.75  75     feed 2005
6      feed.70  70     feed 2000
7  dressing.79  79 dressing 2009
8  dressing.75  75 dressing 2005
9  dressing.70  70 dressing 2000
10     dem2009  79      dem 2009
11     dem2005  75      dem 2005
12     dem2000  70      dem 2000
13       AH414  79  smoking 2009
14       AH108  79      bmi 2009
15        AG58  75      bmi 2005
16       AG384  75  smoking 2005
17        AE69  70      bmi 2000
18       AE320  70  smoking 2000