Last active
March 28, 2024 19:23
-
-
Save erikerhardt/a04938beb26d845873e4e38e915585f5 to your computer and use it in GitHub Desktop.
Convert all character to factor
This file contains 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
iris_e <- iris | |
str(iris_e) | |
# from factor to character | |
iris_e <- iris_e %>% dplyr::mutate(tidyselect::across(tidyselect::where(is.factor), as.character)) | |
str(iris_e) | |
# from character to factor | |
iris_e <- iris_e %>% dplyr::mutate(tidyselect::across(tidyselect::where(is.character), as.factor)) | |
str(iris_e) | |
# remove all-NA columns | |
df2 %>% dplyr::select(tidyselect::where(function(x) any(!is.na(x)))) | |
## Standardize (selected) numeric variables | |
list_var_standardize <- | |
c( | |
"Abeta_1_42" | |
#, "ApoE" | |
, "Alpha_synuclein" | |
, "p_Tau" | |
, "t_Tau" | |
, "p_Tau_log2" | |
, "t_Tau_log2" | |
, Var[["list"]][["var_vol"]] | |
, Var[["list"]][["var_cog"]] | |
#, "ICV" | |
) | |
# means subtracted to center | |
dat_sub_ic %>% | |
dplyr::select( | |
ICV | |
, age_visit | |
, EDUCYRS | |
) %>% | |
dplyr::summarize( | |
ICV = (ICV / 1e6) %>% mean(na.rm = TRUE) | |
, age_visit = age_visit %>% mean(na.rm = TRUE) | |
, EDUCYRS = EDUCYRS %>% mean(na.rm = TRUE) | |
) | |
dat_sub_ic_z <- | |
dat_sub_ic %>% | |
dplyr::mutate( | |
# z-score vol and cog vars | |
across( | |
.cols = all_of(list_var_standardize) | |
, .fns = ~ scale(x = ., center = TRUE, scale = TRUE)[,1] | |
) | |
# center demographic variables | |
#, ICV = scale(ICV / 1e6, center = TRUE, scale = FALSE) %>% as.numeric() | |
#, age_visit = scale(age_visit, center = TRUE, scale = FALSE) %>% as.numeric() | |
, ICV = scale(ICV / 1e6, center = TRUE, scale = TRUE) %>% as.numeric() | |
, age_visit = scale(age_visit, center = TRUE, scale = TRUE) %>% as.numeric() | |
# 12/18/2021 6:19PM comment out to interpret plot | |
#, EDUCYRS = scale(EDUCYRS , center = TRUE, scale = FALSE) %>% as.numeric() | |
, EDUCYRS = scale(EDUCYRS , center = TRUE, scale = TRUE) %>% as.numeric() | |
, GENDER = GENDER - 1 # females are baseline | |
, NP3TOT = scale(NP3TOT, center = TRUE, scale = TRUE) %>% as.numeric() | |
, LEDD_total = scale(LEDD_total, center = FALSE, scale = TRUE) %>% as.numeric() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment