R code conditions2 function return an Error: Unexpected “=” at ” “df2$AETOXGR_STD %in% c(3,4,5)” = 3, paste0(“df2$AETOXGR_STD %in% c(3,4,5) & (“, paste(sapply(ae_caus_columns, function(col) paste0(“toupper(df2$”, col, “) == ‘RELATED'”)), collapse = ” | “), “)”) =” When I run paste of this line independly, it looks good to me though.
Below is complete R code
library(dplyr)
folderPath <- "D:\RAW"
screen <- read.csv(file.path(folderPath, "xxxx_scr.csv"))
ae <- read.csv(file.path(folderPath, "xxxx_ae.csv"))
#AE Summary
screen2 <- screen %>%
filter(toupper(SCRNFYN) %in% c("NO")) %>%
mutate(Country.Name = substr(SiteNumber, 1, 3)) %>%
select(Subject, Country.Name)
df2 <- ae %>%
inner_join(screen2, by = c("Subject"="Subject"))
ae1table <- data.frame(
Category = c(
"Subjects",
"Subjects with Any AE",
"Subjects with G3-5 AE",
"Subjects with Drug Related G3-5 AE",
"Subjects with Serious AE",
"Subjects with Drug Related Serious AE",
"Subjects with AE Associated with Study Drug Discontinuation",
"TEAEs Associated with Dose Reduction",
"AE with dose delay",
"AE Leading to Death",
"Drug Related AE Leading to Death"
),
Overall = numeric(11),
China = numeric(11)
)
total_overall <- nrow(screen2)
total_china <- nrow(screen2[screen2$Country.Name == "CHN", ])
ae1table$Overall[1] <- total_overall
ae1table$China[1] <- total_china
# Identify AECAUS and AEACN columns that end with a numeric value or have no suffix
ae_caus_columns <- grep("^AECAUS[0-9]*$", names(df2), value = TRUE)
ae_acn_columns <- grep("^AEACN[0-9]*$", names(df2), value = TRUE)
conditions2 <- list(
"df2$AETERM != ''" = 2,
"df2$AETOXGR_STD %in% c(3,4,5)" = 3,
paste0("df2$AETOXGR_STD %in% c(3,4,5) & (", paste(sapply(ae_caus_columns, function(col) paste0("toupper(df2$", col, ") == 'RELATED'")), collapse = " | "), ")") = 4,
"df2$AESER_STD == 'Y'" = 5,
paste0("df2$AESER_STD == 'Y' & (", paste(sapply(ae_caus_columns, function(col) paste0("toupper(df2$", col, ") == 'RELATED'")), collapse = " | "), ")") = 6,
paste0("(", paste(sapply(ae_acn_columns, function(col) paste0("toupper(df2$", col, ") == 'DRUG WITHDRAWN'")), collapse = " | "), ")") = 7,
paste0("(", paste(sapply(ae_acn_columns, function(col) paste0("toupper(df2$", col, ") == 'DOSE REDUCED'")), collapse = " | "), ")") = 8,
paste0("(", paste(sapply(ae_acn_columns, function(col) paste0("toupper(df2$", col, ") == 'DOSE DELAYED'")), collapse = " | "), ")") = 9,
"df2$AESDTH == 1" = 10,
paste0("df2$AESDTH == 1 & (", paste(sapply(ae_caus_columns, function(col) paste0("toupper(df2$", col, ") == 'RELATED'")), collapse = " | "), ")") = 11
)
# Apply conditions
for (condition in names(conditions2)) {
index <- conditions2[[condition]]
ae1table$Overall[index] <- length(unique(df2$Subject[eval(parse(text = condition))]))
ae1table$China[index] <- length(unique(df2$Subject[df2$Country.Name == "CHN" & eval(parse(text = condition))]))
}
# Calculate percentages conditionally
ae1table$Overall_Percentage <- ifelse(row.names(ae1table) == 1, NA, sprintf("%.2f%%", (ae1table$Overall / total_overall) * 100))
ae1table$China_Percentage <- ifelse(row.names(ae1table) == 1, NA, sprintf("%.2f%%", (ae1table$China / total_china) * 100))
ae1table <- ae1table[, c("Category", "Overall", "Overall_Percentage", "China", "China_Percentage")]