为什么当sapply子集到R中的数据帧列表时,找不到函数传递的参数



sapply将子集到一个函数内的数据帧列表时,我遇到了一个连线问题,R说"eval(expr,envir,enclos)中的错误:找不到对象'thresh'"。我想知道为什么会发生这种事。

test<-list()
test[[1]]<-as.data.frame(matrix(rnorm(50*5,10,100),50,5))
test[[2]]<-as.data.frame(matrix(rnorm(50*5,10,100),50,5))

findmax<-function(test,thresh){
  print(thresh)
  max(unlist(sapply(test,subset,V1>thresh,select=c("V1"))))
}
findmax(test,thresh=10)

请注意?subset:中的警告

Warning:
     This is a convenience function intended for use interactively.
     For programming it is better to use the standard subsetting
     functions like ‘[’, and in particular the non-standard evaluation
     of argument ‘subset’ can have unanticipated consequences.

subset在其中查找对象和变量的位置上有一些奇怪的评估规则,这取决于调用环境等。当用户在顶级交互调用时,这些规则可以很好地工作,但当封装在函数中时,通常会失败,正如您所发现的那样。

以下是使用标准子设置重写函数的一种方法:

findmax <- function(test, thresh, want) {
    foo <- function(x, thresh, want) {
       take <- x[, want] > thresh
       x[take, want]
    }
    max(unlist(sapply(test, foo, thresh = thresh, want = want)))
}
findmax(test, thresh = 10, want = "V1")

你的测试数据给出的是:

R> findmax(test, thresh = 10, want = "V1")
[1] 230.9756

最新更新