Skip to content

Commit

Permalink
Clin.data_summary table now shows the number of missing values for a…
Browse files Browse the repository at this point in the history
…ll variables except grouping variable(s). If any grouping variable is missing for the given sample, this sample is excluded from the Clin.data_summary table.
  • Loading branch information
lukszafron committed May 25, 2024
1 parent 52bda1a commit 22258ea
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions regression.analyses.r
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,9 @@ res.failed <- subset(res.failed, subset = grepl(Factor, pattern = "(^#.*|^Too (m

dep.var.cats <- sub(dep.vars, pattern = "^.*:", replacement = "")
dep.var.conts <- sub(grep(dep.vars, pattern = ".*:.*", value = T), pattern = ":.*$", replacement = "")
# df <- df[df %>% dplyr::select(all_of(c(dep.var.conts, grep(dep.vars, pattern = ":", value = T, invert = T)))) %>% is.na %>% rowSums() == 0, , drop = F]

if(all(sub.vars != "ALL_SAMPLES")) {
df <- df[df %>% dplyr::select(all_of(sub.vars)) %>% is.na %>% rowSums == 0, , drop = F]
df <- df %>% mutate(across(.cols = c(dep.var.cats, sub.vars), .fns = as.factor))
N <- with(df, aggregate(df %>%
dplyr::select(dep.var.cats, dep.var.conts, other.factors),
Expand All @@ -422,12 +422,12 @@ if(all(sub.vars != "ALL_SAMPLES")) {
by = sapply(sub.vars, FUN = function(x) {get(x)}, simplify = F), FUN = function(y) {if(is.factor(y)) {table(y, useNA = "always")} else {summary_f(y)}})) %>%
as.matrix() %>% as.data.frame(stringsAsFactors = F) %>% mutate(across(.cols = !sub.vars, .fns = as.numeric))

valid.cols <- foreach(i = colnames(df.summary), .combine = c) %do% {
if(i %in% sub.vars) {i} else
if(is.na(sum(df.summary[i]))) {i} else
if(str_contains(x = i, pattern = sub(dep.var.conts, pattern = "(^.*$)", replacement = "\\1\\."), logic = "OR")) {i} else
if(sum(df.summary[i]) >0) {i}
}
valid.cols <- foreach(i = colnames(df.summary), .combine = c) %do% {
if(!is.numeric(df.summary[,i])) {i} else
if(!grepl(i, pattern = "\\.NA('s)?$")) {i} else
if(sum(df.summary[,i]) >0) {i}
}

df.summary <- df.summary[valid.cols]

df.summary.fin <- cbind(df.summary, N)
Expand Down

0 comments on commit 22258ea

Please sign in to comment.