【发布时间】:2019-09-29 23:39:12
【问题描述】:
在我下面的函数中,... 代表用户命名的任何向量(例如,numeric 或 character 等)。例如,用户可能定义age = 1:3 和prof = c("med", "low", "med")。这些额外的向量被添加到名为out 的data.frame。
我想知道是否有一种方法可以创建一个名为 extract 的新参数,以允许用户从最终输出 h 中提取子集。
例如,如果用户想要对age == 2 或age == 2 & prof == "low" 进行子集化,则使用extract = age == 2 & prof == "low" 会返回输出中的相应匹配项?
foo <- function(d, per, ...){ ## Add a new argument called `extract`
out <- data.frame(d, ...)
h <- split(out, rep(seq_along(per), per)) ## `extract` should subset from `h`
return(h)
}
# Example of use:
foo(d = 2:4, per = 1:2, age = 1:3, prof = c("med", "low", "med"))
【问题讨论】:
-
你需要
foo(d = 2:4, per = 1:2, extract =quote(age == 2), age = 1:3, prof = c("med", "low", "med"))和函数内部out1 <- subset(out, subset = eval(extract)) -
你能看看下面的解决方案吗