问题
I can not use the subset argument of any function with mapply. The following calls fail with the subset argument, but they work without:
mapply(ftable,
formula = list(wool ~ breaks,
wool + tension ~ breaks),
subset = list(breaks < 15,
breaks < 20),
MoreArgs = list(data = warpbreaks))
# Error in mapply(ftable, formula = list(wool ~ breaks, wool + tension ~ :
# object 'breaks' not found
mapply(xtabs,
formula = list(~ wool,
~ wool + tension),
subset = list(breaks < 15,
breaks < 20),
MoreArgs = list(data = warpbreaks))
# Error in mapply(xtabs, formula = list(~wool, ~wool + tension), subset = list(breaks < :
# object 'breaks' not found
Map(lm,
formula = list(breaks ~ wool,
breaks ~ tension),
subset = list(breaks < 15,
breaks < 20),
MoreArgs = list(data = warpbreaks))
# Error in mapply(FUN = f, ..., SIMPLIFY = FALSE) :
# object 'breaks' not found
The error seems to be due to subset arguments not being evaluated in the right environment. I know I can subset in the data argument with data = warpbreaks[warpbreaks$breaks < 20, ] as a workaround, but I am looking to improve my knowledge of R.
My questions are:
- Why is the
formulaargument evaluated indata = warpbreaks, but thesubsetargument is not? - I wrote a wrapper around
ftablebecause I need to compute flat tables with frequency and percentage for many variables (more details in my previous questions). Can I modify the wrapper to use thesubsetargument offtableinmapply?
.
# the wrapper
mytable <- function(...) {
mc <- match.call()
mc[[1]] <- quote(expr = ftable)
eval.parent(expr = mc)
# etc
}
来源:https://stackoverflow.com/questions/56817969/non-standard-evaluation-of-subset-argument-with-mapply-in-r