3

関数内の私のコード呼び出し ffwhich は次のとおりです。

library(ffbase)
rm(a,b)
test <- function(x) {
  a <- 1
  b <- 3
  ffwhich(x, x > a & x < b)
}
x <- ff(1:10)
test(x)
Error in eval(expr, envir, enclos) (from <text>#1) : object 'a' not found

traceback()
6: eval(expr, envir, enclos)
5: eval(e)
4: which(eval(e))
3: ffwhich.ff_vector(x, x > a & x < b)
2: ffwhich(x, x > a & x < b) at #4
1: test(x)

遅延評価が原因でしょうか?eval() は、関数テストで制限されている a と b を見つけることができません。関数で ffwhich を使用するにはどうすればよいですか?

  • R 2.15.2
  • ffbase 0.6-3
  • ff 2.2-10
  • OS opensuse 12.2 64 ビット
4

2 に答える 2

4

はい、Arun が示しているような評価の問題のようです。eval のような ffwhich を使用する場合、通常は次のように使用します。

library(ffbase)
rm(a,b)
test <- function(x) {
  a <- 1
  b <- 3
  idx <- x > a & x < b
  idx <- ffwhich(idx, idx == TRUE)
  idx
}
x <- ff(1:10)
test(x)
于 2012-12-28T08:41:00.770 に答える
0

私は同じ問題を抱えていましたが、引数「条件」を関数に渡すことができないため、与えられた答えはそれを解決していませんでした。私はそれを行う方法を手に入れました。ここにあります ::

require(ffdf) 
# the data :: 
x <- as.ffdf( data.frame(a = c(1:4,1),b=5:1))
x[,]

# Now the function below is working :: 

idx_ffdf <- function(data, condition){
 exp <-substitute( (condition) %in% TRUE) 
 # substitute will take the value of condition (non-evaluated). 
 # %in% TRUE makes the condition be false when there is NAs... 
 idx <- do.call(ffwhich, list(data, exp) ) # here is the trick: do.call !!! 
 return(idx)
}
# testing : 
idx <- idx_ffdf(x,a==1)
idx[] # gives the correct 1,5 ...

idx <- idx_ffdf(x,b>3)
idx[] # gives the correct 1,2 ... 

これが誰かに役立つことを願っています!

于 2013-05-23T09:32:19.650 に答える