Skip to content

Instantly share code, notes, and snippets.

@mkim0710
Last active November 17, 2018 12:17
Show Gist options
  • Save mkim0710/60a4f7536a93076696faeb02da5d921f to your computer and use it in GitHub Desktop.
Save mkim0710/60a4f7536a93076696faeb02da5d921f to your computer and use it in GitHub Desktop.
ExampleData4Time2Censor
#@ ExampleData4Time2Censor = read_csv(url("https://raw.githubusercontent.com/mkim0710/tidystat/master/data/ExampleData4Time2Censor.csv")) =====
library(tidyverse)
ExampleData4Time2Censor = read_csv(url("https://raw.githubusercontent.com/mkim0710/tidystat/master/data/ExampleData4Time2Censor.csv"))
ExampleData4Time2Censor %>% dput
ExampleData4Time2Censor %>% str(give.attr = F) #----
ExampleData4Time2Censor %>% as.tibble # ----
# > ExampleData4Time2Censor %>% dput
# structure(list(PERSON_ID = 1:20, AGE_GROUP = c(15L, 15L, 15L,
# 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
# 15L, 15L, 15L, 15L), DTH_YM = structure(c(NA, NA, 15399, 15705,
# 15917, 15583, NA, NA, NA, NA, NA, NA, NA, NA, 16039, 15764, NA,
# NA, 15917, NA), class = "Date"), defineDate.MDD = structure(c(15330,
# NA, NA, NA, NA, NA, 14030, NA, NA, NA, NA, 14492, NA, NA, NA,
# NA, NA, 14896, NA, NA), class = "Date"), defineDate.Bip = structure(c(15712,
# NA, 14616, 14567, NA, 14914, 14314, 16035, 15785, NA, 14944,
# 15061, NA, NA, NA, 15615, NA, 15335, NA, 14356), class = "Date"),
# defineDate.Sch = structure(c(NA, 15922, NA, NA, 15156, NA,
# NA, NA, NA, 15857, NA, 15112, 14043, 15188, 15224, NA, 14999,
# NA, 14975, NA), class = "Date"), EndTime = structure(c(15330,
# 15922, 14616, 14567, 15156, 14914, 14030, 16035, 15785, 15857,
# 14944, 14492, 14043, 15188, 15224, 15615, 14999, 14896, 14975,
# 14356), class = "Date"), EndTime.is.MDD = c(TRUE, FALSE,
# FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE,
# TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE
# )), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"
# ), spec = structure(list(cols = list(PERSON_ID = structure(list(), class = c("collector_integer",
# "collector")), AGE_GROUP = structure(list(), class = c("collector_integer",
# "collector")), DTH_YM = structure(list(format = ""), class = c("collector_date",
# "collector")), defineDate.MDD = structure(list(format = ""), class = c("collector_date",
# "collector")), defineDate.Bip = structure(list(format = ""), class = c("collector_date",
# "collector")), defineDate.Sch = structure(list(format = ""), class = c("collector_date",
# "collector")), EndTime = structure(list(format = ""), class = c("collector_date",
# "collector")), EndTime.is.MDD = structure(list(), class = c("collector_logical",
# "collector"))), default = structure(list(), class = c("collector_guess",
# "collector"))), class = "col_spec"))
# > ExampleData4Time2Censor %>% str(give.attr = F) #----
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 20 obs. of 8 variables:
# $ PERSON_ID : int 1 2 3 4 5 6 7 8 9 10 ...
# $ AGE_GROUP : int 15 15 15 15 15 15 15 15 15 15 ...
# $ DTH_YM : Date, format: NA NA "2012-02-29" "2012-12-31" ...
# $ defineDate.MDD: Date, format: "2011-12-22" NA NA NA ...
# $ defineDate.Bip: Date, format: "2013-01-07" NA "2010-01-07" "2009-11-19" ...
# $ defineDate.Sch: Date, format: NA "2013-08-05" NA NA ...
# $ EndTime : Date, format: "2011-12-22" "2013-08-05" "2010-01-07" "2009-11-19" ...
# $ EndTime.is.MDD: logi TRUE FALSE FALSE FALSE FALSE FALSE ...
# > ExampleData4Time2Censor %>% as.tibble # ----
# # A tibble: 20 x 8
# PERSON_ID AGE_GROUP DTH_YM defineDate.MDD defineDate.Bip defineDate.Sch EndTime EndTime.is.MDD
# <int> <int> <date> <date> <date> <date> <date> <lgl>
# 1 1 15 NA 2011-12-22 2013-01-07 NA 2011-12-22 TRUE
# 2 2 15 NA NA NA 2013-08-05 2013-08-05 FALSE
# 3 3 15 2012-02-29 NA 2010-01-07 NA 2010-01-07 FALSE
# 4 4 15 2012-12-31 NA 2009-11-19 NA 2009-11-19 FALSE
# 5 5 15 2013-07-31 NA NA 2011-07-01 2011-07-01 FALSE
# 6 6 15 2012-08-31 NA 2010-11-01 NA 2010-11-01 FALSE
# 7 7 15 NA 2008-05-31 2009-03-11 NA 2008-05-31 TRUE
# 8 8 15 NA NA 2013-11-26 NA 2013-11-26 FALSE
# 9 9 15 NA NA 2013-03-21 NA 2013-03-21 FALSE
# 10 10 15 NA NA NA 2013-06-01 2013-06-01 FALSE
# 11 11 15 NA NA 2010-12-01 NA 2010-12-01 FALSE
# 12 12 15 NA 2009-09-05 2011-03-28 2011-05-18 2009-09-05 TRUE
# 13 13 15 NA NA NA 2008-06-13 2008-06-13 FALSE
# 14 14 15 NA NA NA 2011-08-02 2011-08-02 FALSE
# 15 15 15 2013-11-30 NA NA 2011-09-07 2011-09-07 FALSE
# 16 16 15 2013-02-28 NA 2012-10-02 NA 2012-10-02 FALSE
# 17 17 15 NA NA NA 2011-01-25 2011-01-25 FALSE
# 18 18 15 NA 2010-10-14 2011-12-27 NA 2010-10-14 TRUE
# 19 19 15 2013-07-31 NA NA 2011-01-01 2011-01-01 FALSE
# 20 20 15 NA NA 2009-04-22 NA 2009-04-22 FALSE
ExampleData4Time2Censor = ExampleData4Time2Censor %>%
mutate(
EndTime.new = {pmin(as.Date("2013-12-31"), DTH_YM, defineDate.MDD, defineDate.Bip, defineDate.Sch, na.rm = T)}
, EndTime.is.MDD.new = if_else(is.na(EndTime == defineDate.MDD), F, EndTime == defineDate.MDD)
)
ExampleData4Time2Censor %>% {identical(.$EndTime, .$EndTime.new)} #----
ExampleData4Time2Censor %>% {identical(.$EndTime.is.MDD, .$EndTime.is.MDD.new)} #----
# > ExampleData4Time2Censor %>% {identical(.$EndTime, .$EndTime.new)} #----
# [1] TRUE
# > ExampleData4Time2Censor %>% {identical(.$EndTime.is.MDD, .$EndTime.is.MDD.new)} #----
# [1] TRUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment