[英]subset from a list of data.frames in R
In my function below ...
represents any vectors (eg, numeric
or character
etc.) named by the user. 在下面的函数中,
...
表示用户命名的任何矢量(例如, numeric
或character
等)。 For example, a user might define age = 1:3
and prof = c("med", "low", "med")
. 例如,用户可以定义
age = 1:3
和prof = c("med", "low", "med")
。 These extra vectors are added to a data.frame
called out
. 这些额外的矢量被添加到一个名为
out
的data.frame
out
。
I was wondering if there is a way I could create a new argument called extract
to allow user to subset from the final output h
. 我想知道是否有办法创建一个名为
extract
的新参数,以允许用户从最终输出h
中子集化。
For example, if a user wants to subset age == 2
or age == 2 & prof == "low"
the corresponding match from the output is returned by use of extract = age == 2 & prof == "low"
? 例如,如果用户希望将
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"))
This does not use any packages nor does it explicitly use eval
. 这不使用任何包,也不显式使用
eval
。
foo2 <- function(d, per, ..., extract = TRUE) {
out <- data.frame(...)
h <- split(out, rep(seq_along(per), per))
s <- substitute(extract)
lapply(h, function(x) do.call("subset", list(x, s)))
}
foo2(d = 2:4, per = 1:2, age = 1:3, prof = c("med", "low", "med"), extract = age == 2)
We can pass the quoted expression in 'extract' and eval
uate to filter the rows 我们可以在'extract'中传递带引号的表达式并
eval
以过滤行
library(tidyverse)
foo <- function(d, per, extract, ...){ ## Add a new argument called `extract`
extract <- rlang::enexpr(extract)
out <- data.frame(d, ...)
h <- split(out, rep(seq_along(per), per))
map(h, ~ .x %>%
filter(!! extract))
}
foo(d = 2:4, per = 1:2, extract = age == 2, age = 1:3, prof = c("med", "low", "med"))
#$`1`
#[1] d age prof
#<0 rows> (or 0-length row.names)
#$`2`
# d age prof
#1 3 2 low
Or using base R
或使用
base R
foo <- function(d, per, extract, ...){ ## Add a new argument called `extract`
extract <- substitute(extract)
out <- data.frame(d, ...)
h <- split(out, rep(seq_along(per), per))
lapply(h, function(x) subset(x, subset = eval(extract)))
}
foo(d = 2:4, per = 1:2, extract = age == 2, age = 1:3, prof = c("med", "low", "med"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.