【发布时间】:2017-10-24 04:52:26
【问题描述】:
我有一个混合数据类型的函数。它以数据框和字符串变量作为输入参数。
library(dplyr)
myfunc <- function (dat=NULL,species=NULL,sepal_thres=NULL) {
dat %>%
filter(Species==species & Sepal.Length <= sepal_thres)
}
myfunc(dat=iris,species="virginica",sepal_thres=5)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 4.9 2.5 4.5 1.7 virginica
但我想将它与向量列表一起应用
species_vecs <- c("virginica","setosa")
sepal_thres_vecs <- c(5, 6)
purrr::pmap(list(dat=iris, species=species_vecs, sepal_thres=sepal_thres_vecs), myfunc)
我收到了这个错误:
Error: Element 2 has length 2, not 1 or 5.
正确的做法是什么?
species 和 sepal_tres 参数并不是取自这个组合:
> expand.grid(species_vecs,sepal_thres_vecs) %>% rename(species=Var1, sepal_thres=Var2)
species sepal_thres
1 virginica 5
2 setosa 5
3 virginica 6
4 setosa 6
但是dat作为参数是固定的。
【问题讨论】: