#' function: Clean data for analysis:
#' first version of recode gurland data (dementia transition status)
#'
#' @param dat DataFrame. should be cleaned till recode.
#' @param targets list. target lists to recode. e.g. list(c('CN.CN.Dement.CN'),c('CN.Dement.CN.CN'))
#' @return Final recoded data.
#' @examples TBA
#' @author Jongwoo Choi, \email{jc4816@columbia.edu}
#' @references TBA
#' @keywords recode data analysis
#' @import dplyr
#' @import knitr
#' @import reshape2
#' @export
transition_recode <- function(dat=dat.clean, targets=targets){
# There's reversion to non-demented in the follow-up. We took their last avaialble data as their status. We recoded as follows:
# `CN. CN. Dementia. CN` and `CN. Dememtia. CN. CN` -> `CN. CN. CN. CN`.
# `CN. Dementia. CN. NA` -> `CN. CN. CN. NA`.
# `CN. Dementia. Dementia. CN` -> `CN. CN. CN. CN`.
ccd01.long = dat.clean %>% select(id_num, eval, ccd01)
ccd01.wide = dcast(ccd01.long, id_num ~ eval)
ccd01.wide$transition <- factor(apply(ccd01.wide[,-1], 1, function(x) paste0(x, collapse='.')))
# sjt.xtab(ccd01.wide$transition, ccd01.wide$`0`, show.na =TRUE)
target_id_num <- list()
for(i in 1:length(targets)){
target_id_num[[i]] <- ccd01.wide$id_num[ccd01.wide$transition %in% targets[[i]]]
dat.clean$ccd01[dat.clean$id_num %in% target_id_num[[i]]] <-
ifelse(dat.clean$ccd01[dat.clean$id_num %in% target_id_num[[i]]] =='Dement', 'CN', 'CN')} # change Dement to CN
dat.clean.recoded <- dat.clean
return(dat.clean.recoded)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.