I am trying to subset data within lapply for different columns to produce repeated single arm meta-analysis using metaprop
command for many outcomes in 1 step as I have a giant number of outcomes.
Some outcomes have empty cells, so I need to properly subset data within lapply
Here is my data and code:
############################### Data sample #####################################
data<- read.table(text="
studlab Number.of.patients Procedure Mortality Post.op.stroke
Lee2015 15 SPT 0 0
Wang2019 44 SPT 1
Nachira2018 12 SPT 1 1
Zhang2020 20 SPT 1 1
Lee2017 48 SPT 0 1
Lv2018 10 SPT 0 0
Weiping2018 28 SPT 0 0
Liu2021 60 SPM 0
Gan2020 56 SPM
Ye2021 105 SPM 1 1
Takeda2021 12 SPM
Yin2020 22 SPM 0
Gan2020 28 SPM
Egberts2019 4 SPM 1 1
Wang2019 80 SPM 0
Fujiwara2017 60 SPM 0 1
Mori2017 7 SPM 0 0
Parker2011 8 SPM 0 1
Zhu2021 19 SPM 0 1", header=T, sep="\t")
############################ Same data with dput command ####################
dput(data)
structure(list(studlab = c("Lee2015", "Wang2019", "Nachira2018",
"Zhang2020", "Lee2017", "Lv2018", "Weiping2018", "Liu2021", "Gan2020",
"Ye2021", "Takeda2021", "Yin2020", "Gan2020", "Egberts2019",
"Wang2019", "Fujiwara2017", "Mori2017", "Parker2011", "Zhu2021"
), Number.of.patients = c(15L, 44L, 12L, 20L, 48L, 10L, 28L,
60L, 56L, 105L, 12L, 22L, 28L, 4L, 80L, 60L, 7L, 8L, 19L), Procedure = c("SPT",
"SPT", "SPT", "SPT", "SPT", "SPT", "SPT", "SPM", "SPM", "SPM",
"SPM", "SPM", "SPM", "SPM", "SPM", "SPM", "SPM", "SPM", "SPM"
), Mortality = c(0L, NA, 1L, 1L, 0L, 0L, 0L, 0L, NA, 1L, NA,
NA, NA, 1L, NA, 0L, 0L, 0L, 0L), Post.op.stroke = c(0L, 1L, 1L,
1L, 1L, 0L, 0L, NA, NA, 1L, NA, 0L, NA, 1L, 0L, 1L, 0L, 1L, 1L
)), class = "data.frame", row.names = c(NA, -19L))
############################### Used code #####################################
library(tidyr); library(ggplot2); library(meta); library(metafor)
lapply(names(data)[4:5], function(columntoplot){
## To subset based on the outcome column ##
df <-subset( data, data[[columntoplot ]] >=0 ) ## NOT WORKING PROPERLY
mp<-metaprop(columntoplot,Number.of.patients, data=df, studlab=studlab, method = "Inverse",method.tau = "DL");mp
mp2<- update (mp, byvar=Procedure);mp2
pdf(filename = paste0(graphname, ".pdf"), width = 20, height = 20)
forest (mp ); forest (mp2 )
dev.off()
})
#################################################################################
CodePudding user response:
There are a couple of issues with your code.
- In
metaprop
, theevent
andn
arguments must be symbols (notcharacter
strings) referring to columns indata
. - As you correctly identified, the
subset
command is syntactically incorrect. - The filename argument inside
pdf
isfile =
, notfilename =
. - I strongly recommend using a linter to ensure consistent code formatting & indentation. Or at the very least, adopt & stick to a style guide to increase the readability of your code.
As to your question, the following seems to work.
# Variables of interest
vars <- names(data)[4:5]
# Reshape `data` from wide to long for the variables of interest
df <- data %>% pivot_longer(all_of(vars))
# Since the loop is not returning anything and only generates plots,
# use `purrr::walk` to loop through all variables of interest.
vars %>%
walk(function(var) {
# Meta-analysis
mp <- metaprop(
event = value,
n = Number.of.patients,
studlab = studlab,
data = df %>% filter(name == var, !is.na(value)),
method = "Inverse",
method.tau = "DL")
mp2 <- update(mp, byvar = Procedure)
#Plot
pdf(file = paste0(var, ".pdf"), width = 20, height = 20)
forest(mp)
forest(mp2)
dev.off()
})