2

尝试应用于match.fun其他函数中定义的函数时出现错误。

x <- matrix(rnorm(10*100), nrow=100) # data sample
descStats <- function(x, stats = c("n", "min", "max", "srange", "mean", "median", "sd")) {
  n <- function(x, ...) sum(!is.na(x), ...)
  srange <- function(x, ...) max(x, ...) - min(x, ...)
  fun <- function(x) {
    result <- vapply(stats, function(z) match.fun(z)(x, na.rm=TRUE), FUN.VALUE=numeric(1))
  }
  if (is.vector(x)) {
    result <- fun(x)
  }
  if (is.matrix(x) || is.data.frame(x)) {
    result <- t(apply(x, 2, fun))
  }
  return(result)
}
descStats(x)
## Error in get(as.character(FUN), mode = "function", envir = envir) : 
##   object 'n' of mode 'function' was not found

如果我在 descStats 函数之外定义它工作正常nsrange

n <- function(x, ...) sum(!is.na(x), ...)
srange <- function(x, ...) max(x, ...) - min(x, ...)
descStats2 <- function(x, stats = c("n", "min", "max", "srange", "mean", "median", "sd")) {
  fun <- function(x) {
    result <- vapply(stats, function(z) match.fun(z)(x, na.rm=TRUE), FUN.VALUE=numeric(1))
  }
  if (is.vector(x)) {
    result <- fun(x)
  }
  if (is.matrix(x) || is.data.frame(x)) {
    result <- t(apply(x, 2, fun))
  }
  return(result)
}
descStats2(x)
##         n       min      max   srange        mean      median        sd
##  [1,] 100 -2.303839 2.629366 4.933205  0.03711611  0.14566523 1.0367947
##  [2,] 100 -1.968923 2.169382 4.138305 -0.03917503  0.02239458 0.9048509
##  [3,] 100 -2.365891 2.424077 4.789968 -0.08012138 -0.23515910 1.0438133
##  [4,] 100 -2.740045 2.127787 4.867832  0.03978241  0.15363449 0.9778891
##  [5,] 100 -1.598295 2.603525 4.201820  0.23796616  0.16376239 1.0428915
##  [6,] 100 -1.550385 1.684155 3.234540 -0.11114479 -0.09264598 0.8260126
##  [7,] 100 -2.438641 3.268796 5.707438  0.02948100 -0.05594740 1.0481331
##  [8,] 100 -1.716407 2.795340 4.511747  0.22463606  0.16296613 0.9555129
##  [9,] 100 -2.359165 1.975993 4.335158 -0.33321888 -0.17580933 0.9784788
## [10,] 100 -2.139267 2.838986 4.978253  0.15540182  0.07803265 1.0149671

它的另一种使用方式eval(call(FUN, args))。例如。

descStats3 <- function(x, stats = c("n", "min", "max", "srange", "mean", "median", "sd")) {
  n <- function(x, ...) sum(!is.na(x), ...)
  srange <- function(x, ...) max(x, ...) - min(x, ...)
  fun <- function(x) {
    result <- vapply(stats, function(z) eval(call(z, x, na.rm=TRUE)), FUN.VALUE=numeric(1))
  }
  if (is.vector(x)) {
    result <- fun(x)
  }
  if (is.matrix(x) || is.data.frame(x)) {
    result <- t(apply(x, 2, fun))
  }
  return(result)
}
descStats3(x)
##         n       min      max   srange        mean      median        sd
##  [1,] 100 -2.303839 2.629366 4.933205  0.03711611  0.14566523 1.0367947
##  [2,] 100 -1.968923 2.169382 4.138305 -0.03917503  0.02239458 0.9048509
##  [3,] 100 -2.365891 2.424077 4.789968 -0.08012138 -0.23515910 1.0438133
##  [4,] 100 -2.740045 2.127787 4.867832  0.03978241  0.15363449 0.9778891
##  [5,] 100 -1.598295 2.603525 4.201820  0.23796616  0.16376239 1.0428915
##  [6,] 100 -1.550385 1.684155 3.234540 -0.11114479 -0.09264598 0.8260126
##  [7,] 100 -2.438641 3.268796 5.707438  0.02948100 -0.05594740 1.0481331
##  [8,] 100 -1.716407 2.795340 4.511747  0.22463606  0.16296613 0.9555129
##  [9,] 100 -2.359165 1.975993 4.335158 -0.33321888 -0.17580933 0.9784788
## [10,] 100 -2.139267 2.838986 4.978253  0.15540182  0.07803265 1.0149671
identical(descStats2(x), descStats3(x))
## [1] TRUE

为什么descStats不工作?

4

3 回答 3

5

编写自己的match.fun. 我调用了我的函数fget以表明它是get专门为函数设计的版本,因此遵守函数的常规范围规则。(如果您不确定它们是什么,请考虑以下代码c <- 10; c(c, 5):)

#' Find a function with specified name.
#'
#' @param name length one character vector giving name
#' @param env environment to start search in.
#' @examples
#' c <- 10
#' fget("c")
fget <- function(name, env = parent.frame()) {
  if (identical(env, emptyenv())) {
    stop("Could not find function called ", name, call. = FALSE)
  }

  if (exists(name, env, inherits = FALSE) && is.function(env[[name]])) {
    env[[name]]
  } else {
    fget(name, parent.env(env))
  }
}

实现是一个简单的递归函数:基本情况是emptyenv(),每个环境的最终祖先,并且对于沿父堆栈的每个环境,我们检查是否name存在一个名为的对象,并且它是一个函数。

它适用于@nograpes 提供的简单测试用例,因为环境默认为调用环境:

fun <- function(x) {
  n <- sum
  fget('n')(x)
}
fun(10)
# [1] 10
于 2013-01-09T14:42:53.930 回答
4

这是一个范围问题。查看 match.fun 的代码,您会得到答案。

match.fun范围是 envir <- parent.frame(2)

get范围在 envir = as.environment(-1) = parent.frame(1)

我认为我们不能将 envir 作为参数传递。一种解决方案是使用@nograpes (不安全)提出的 get 或破解match.fun和更改

envir <- parent.frame(2)envir <- parent.frame(1)

于 2013-01-06T16:24:41.120 回答
1

由于我还不完全理解的原因,如果您使用get而不是match.fun,一切正常。

x <- matrix(rnorm(10*100), nrow=100) # data sample
descStats <- function(x, stats = c("n", "min", "max", "srange", "mean", "median", "sd")) {
  n <- function(x, ...) sum(!is.na(x), ...)
  srange <- function(x, ...) max(x, ...) - min(x, ...)
  fun <- function(x) {
    # get added here.
    result <- vapply(stats, function(z) get(z)(x, na.rm=TRUE), FUN.VALUE=numeric(1))
  }
  if (is.vector(x)) {
    result <- fun(x)
  }
  if (is.matrix(x) || is.data.frame(x)) {
    result <- t(apply(x, 2, fun))
  }
  return(result)
}
descStats(x)
于 2013-01-06T16:10:13.833 回答