【发布时间】:2017-07-24 14:58:18
【问题描述】:
用接受向量、静态值和NULLs 的参数对 R 函数进行向量化的最佳方法是什么?当我 Map() 一个带有参数的函数时遇到问题,这些参数有时由 NULLs 提供。我收到以下错误消息(使用下面的代码复制):
Error in mapply(FUN = f, ..., SIMPLIFY = FALSE) :
zero-length inputs cannot be mixed with those of non-zero length
为了重现这个问题,我编写了一个函数,它使用来自data 的参数返回n 模拟值,并可选择实现min 和max 值。
#' foo (example function with some args defaulting to NULL)
#'
#' Returns simulated normal values using population parameters from data
#'
#' @param data Numeric vector used to calculate population parameters
#' @param n Number of simulated data points to return
#' @param min Optional. Creates a truncation effect. Simulated values
#' below min will be replaced with min.
#' @param max Optional. Creates a truncation effect. Simulated values
#' above max will be replaced with max.
#' @return Numeric vector of simulated values.
foo <- function(data, n, min = NULL, max = NULL) {
x <- rnorm(n, mean(data), sd(data))
if (!is.null(min)) {
x[x < min] <- min
}
if (!is.null(max)) {
x[x > max] <- max
}
x
}
我正在使用列表并希望该函数返回列表。所以,这里的数据向量是一个数值向量的列表。
## data vector
data <- replicate(5, rnorm(3), simplify = FALSE)
其他参数可以接受静态 (length(x) == 1) 或动态值 (length(x) == length(data))。当提供非 NULL 值时,无论 args 被赋予一个还是多个值,它都有效。
## static args (this works)
n <- 10
min <- -1.96
max <- 1.96
Map(foo, data, n, min, max)
## vector args (this works)
n <- sample(2:100, 5)
min <- runif(5, -4, -1)
max <- runif(5, 1, 4)
Map(foo, data, n, min, max)
但是当向 args 传递 NULL 值时,它会中断。
## null args (this doesn't work)
n <- sample(2:100, 5)
min <- NULL
max <- NULL
Map(foo, data, n, min, max)
## it doesn't matter if n is a vector
n <- 10
min <- NULL
max <- NULL
Map(foo, data, n, min, max)
Error in mapply(FUN = f, ..., SIMPLIFY = FALSE) :
zero-length inputs cannot be mixed with those of non-zero length
【问题讨论】:
-
您想在输入列表中使用 each 向量的
mean吗?还是输入列表中所有个向量(聚合)的mean? -
这不是我要修复的实际功能,但相当于假设我想独立使用每个向量。
-
因为在你的函数中,你声明了
min=NULL和max=NULL,你可以避免在你的调用中不传递min和max。使用Map(foo,data,10)对我有用。 -
这是作为一个函数使用的,所以有时它会被赋予 NULL 有时不会。我意识到我可以为每个不包含潜在的
NULL值的排列创建不同的Map()调用,但是有问题的实际函数有几个默认为NULL 的参数,所以我宁愿一次性完成. -
改为传递
Map(foo,data,10,NA,NA)?min <- NA和max <- NA...
标签: r