This function creates a derived variable (pct_time_der) that provides an estimated percentage of the time a person's life was spent in Canada.
pct_time_fun(DHHGAGE_cont, SDCGCBG, SDCGRES)
Numeric value between 0 and 100 that represents percentage of a respondent's time in Canada
Since SDCGRES is a categorical variable measuring length of time, we've set midpoints in the function. A respondent identified as being in Canada for 0-9 years is assigned a value of 4.5 years, and someone who has been in Canada for over 10 years is assigned a value of 15 years.
# Using pct_time_fun() to create percent time values between CCHS cycles
# pct_time_fun() is specified in variable_details.csv along with the CCHS
# variables and cycles included.
# To transform pct_time_der across cycles, use rec_with_table() for each CCHS
# cycle and specify pct_time_der, along with age (DHHGAGE_cont), whether or
# not someone was born in Canada (SDCGCBG), how long someone has lived in
# Canada (SDCGRES). Then by using merge_rec_data(), you can combine
# pct_time_der across cycles
library(cchsflow)
pct_time2009_2010 <- rec_with_table(
cchs2009_2010_p, c(
"DHHGAGE_cont", "SDCGCBG",
"SDCGRES", "pct_time_der"
)
)
#> No variable_details detected.
#> Loading cchsflow variable_details
#> Using the passed data variable name as database_name
#> NOTE for SDCGCBG: CCHS 2001 does not have don't know (7) or refusal (8)
#> NOTE for SDCGRES: CCHS 2001 missing don't know (7), refusal (8)
head(pct_time2009_2010)
#> DHHGAGE_cont SDCGCBG SDCGRES pct_time_der
#> 1 13 1 NA(a) 100.00000
#> 2 27 2 2 55.55556
#> 3 62 1 NA(a) 100.00000
#> 4 52 1 NA(a) 100.00000
#> 5 67 1 NA(a) 100.00000
#> 6 62 1 NA(a) 100.00000
pct_time2011_2012 <- rec_with_table(
cchs2011_2012_p, c(
"DHHGAGE_cont", "SDCGCBG",
"SDCGRES", "pct_time_der"
)
)
#> No variable_details detected.
#> Loading cchsflow variable_details
#> Using the passed data variable name as database_name
#> NOTE for SDCGCBG: CCHS 2001 does not have don't know (7) or refusal (8)
#> NOTE for SDCGRES: CCHS 2001 missing don't know (7), refusal (8)
tail(pct_time2011_2012)
#> DHHGAGE_cont SDCGCBG SDCGRES pct_time_der
#> 195 72.0 2 2 20.83333
#> 196 22.0 1 NA(a) 100.00000
#> 197 18.5 1 NA(a) 100.00000
#> 198 42.0 2 2 35.71429
#> 199 62.0 1 NA(a) 100.00000
#> 200 52.0 1 NA(a) 100.00000
combined_pct_time <- merge_rec_data(pct_time2009_2010, pct_time2011_2012)
head(combined_pct_time)
#> DHHGAGE_cont SDCGCBG SDCGRES pct_time_der
#> 1 13 1 NA(a) 100.00000
#> 2 27 2 2 55.55556
#> 3 62 1 NA(a) 100.00000
#> 4 52 1 NA(a) 100.00000
#> 5 67 1 NA(a) 100.00000
#> 6 62 1 NA(a) 100.00000
tail(combined_pct_time)
#> DHHGAGE_cont SDCGCBG SDCGRES pct_time_der
#> 395 72.0 2 2 20.83333
#> 396 22.0 1 NA(a) 100.00000
#> 397 18.5 1 NA(a) 100.00000
#> 398 42.0 2 2 35.71429
#> 399 62.0 1 NA(a) 100.00000
#> 400 52.0 1 NA(a) 100.00000
# Using pct_time_fun() to generate a value for percent time spent in Canada
# with user inputted values Let's say you are 27 years old who was born
# outside of Canada and have been living in Canada for less than 10 years.
# Your estimated percent time spent in Canada can be calculated as follows:
pct_time <- pct_time_fun(DHHGAGE_cont = 27, SDCGCBG = 2, SDCGRES = 1)
print(pct_time)
#> [1] 16.66667