Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update metalite_table1.R #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

Emilylyu99
Copy link

Handling missing value when group contain missing values

The code is following:
install.packages("metalite.table1")
library(metalite.table1)
metalite_table1(~ AGE + SEX + RACE + BMIBLGR1 | ARM, data = r2rtf::r2rtf_adsl, id = "USUBJID")
library(dplyr)

handle_missing_group <- function(data, group, missing_group) {
if (missing_group == "ignore") {
data <- data[!is.na(data[[group]]), ]
message("Ignore missing values in the group variable.")
} else if (missing_group == "count") {
data[[group]] <- addNA(data[[group]], ifany = TRUE)
levels(data[[group]]) <- c(levels(data[[group]]), "Missing")
} else if (missing_group == "display") {
data[[group]] <- addNA(data[[group]], ifany = TRUE)
levels(data[[group]]) <- c(levels(data[[group]]), "Missing")
} else {
stop("Invalid value for 'missing_group'.")
}

return(data)
}
data <- r2rtf::r2rtf_adsl

updated_data <- data %>%
mutate(
AGE = if_else(runif(n()) < 0.1, NA, AGE),
SEX = if_else(runif(n()) < 0.04, NA, SEX),
RACE = if_else(runif(n()) < 0.07, NA, RACE),
BMIBLGR1 = if_else(runif(n()) < 0.02, NA, BMIBLGR1)
)
updated_data <- handle_missing_group(updated_data, "AGE", "count")

metalite_table1(~ AGE + SEX + RACE + BMIBLGR1 | ARM, data = updated_data, id = "USUBJID")

we can change the "updated_data <- handle_missing_group(updated_data, "AGE", "count")" to "updated_data <- handle_missing_group(updated_data, "AGE", "display")" or "updated_data <- handle_missing_group(updated_data, "AGE", "ignore")".

when we fill "updated_data <- handle_missing_group(updated_data, "AGE":
截屏2024-06-05 下午12 47 45

Handling missing value when group contain missing values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant