I'm helping a colleague with an error in her code. We want to obtain frequencies of units with valid values across 4 variables. She used ifelse to flag units meeting her criteria, then used summarytools::freq to obtain a summary frequency table. The nested code (initial run) generates the following error: Error : length(name) == 1 is not TRUE
Can someone help me understand what's happening here?
When the ifelse is separated out, the step-by-step approach works without a problem (labelled b). I also, by accident, found that including an additional set of parentheses around the ifelse did away with the error (labelled c)
#create some data
x <- data.frame("hs.risk.old" = NA, "hs.risk.new" = c(48,42,35), "ms.risk.old" = NA, "ms.risk.new" = c(52,57,NA))
#conduct summary with steps separated
x$flag <- ifelse(!is.na(x$hs.risk.new) | !is.na(x$ms.risk.new) | !is.na(x$hs.risk.old) | !is.na(x$ms.risk.old),1,0)
freq_x <- data.frame(summarytools::freq(x$flag))
#summary nested gives error and warning
freq_xb <- data.frame(summarytools::freq(ifelse(!is.na(x$hs.risk.new) | !is.na(x$ms.risk.new) | !is.na(x$hs.risk.old) | !is.na(x$ms.risk.old),1,0)))
#summary with extra parentheses gives no error, but still warning
freq_xc <- data.frame(summarytools::freq((ifelse(!is.na(x$hs.risk.new) | !is.na(x$ms.risk.new) | !is.na(x$hs.risk.old) | !is.na(x$ms.risk.old),1,0))))