【发布时间】:2013-02-09 02:26:01
【问题描述】:
请考虑这个函数:
tf <- function(formula = NULL, data = NULL, groups = NULL) {
grv <- eval(substitute(groups), data, environment(formula)) # the values
grn <- as.character(match.call()$groups) # the name
gr <- match.call()$groups # unquoted name
p <- xyplot(formula, data, # draws the data but not in groups
# Try these options:
# p <- xyplot(formula, data, groups, # can't find 'cat2'
# p <- xyplot(formula, data, groups = data[,grn], # can't fine grn
# p <- xyplot(formula, data, groups = grv, # can't find grv
panel = function(x, y) {
panel.stripplot(x, y, jitter.data = TRUE, pch = 20)
}
)
p
}
你可以运行的:
tf(formula = mpg~vs, groups = am, data = mtcars)
在将 groups 参数传递给 xyplot 时我做错了什么 - 为什么找不到?我无法弄清楚它如何想要group 信息。谢谢。
更新:
@agstudy 的回答很有帮助,但是如果我像原来的例子一样添加面板功能,仍然无法识别组(没有分组,但也没有发生错误):
tf <- function(formula = NULL, data = NULL, groups = NULL) {
ll <- as.list(match.call(expand.dots = FALSE)[-1])
p <- xyplot(as.formula(ll$formula),
data = eval(ll$data),
groups = eval(ll$groups),
panel = function(x, y) {
panel.stripplot(x, y, jitter.data = TRUE, pch = 20)
}
)
p
}
有些东西还是不见了……谢谢。
【问题讨论】:
-
关于这是否是lattice的“特征”;我确实相信这种行为是允许将变量名称作为
groups参数放入数据集中的功能的副产品。这需要一点 R 体操,我认为副产品是你想要在这里做的事情更加困难。我知道我之前至少曾为此挣扎过一次,并希望我现在有更多时间再次研究它;这是一个有趣的问题! -
谢谢。如您所知,这些范围界定问题经常让我烦恼!