0

I have a function that build a logical condition dynamically according to parameter (that determines the predicate length). Here's the code:

predicate <- function(pred.size){

predicate.result <- ""

for(s in 1:pred.size){

predicate.result <- paste (predicate.result,data.clean.eval[j,top.pred.size.predictions[[s]][1]] , " == TRUE ", sep = " ")
if (!s==pred.size) predicate.result <- paste (predicate.result, " OR ")
}

if (paste(predicate.result)) return TRUE else RETURN FALSE
}

however, pred.result return a string:

predicate.result [1] " FALSE == TRUE OR FALSE == TRUE " and when coming to test the condition if (paste(predicate.result)) return TRUE else RETURN FALSE R return error: Error: unexpected numeric constant in "if (paste(predicate.result)) return TRUE"

any idea how to test that predicate and make this function return TRUE/FALSE ?

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
user3628777
  • 529
  • 3
  • 10
  • 20
  • You have several functions in there that are not in the base package. Please give their package source. Now, there's a fundamental error: `paste` does not return a value which can be coerced to `logical`, so your last `if` condition will fail with error. What do you want to test `predicate.result` for? – Carl Witthoft May 14 '14 at 11:27
  • predicate.result is a string which represents a logical predicate consists of s arguments of the form: a1 == TRUE OR a2 == TRUE ... and so on - s times. each of these arguments is either TRUE or FALSE. The final test : "if (paste(predicate.result)) return TRUE else RETURN FALSE" is meant to evaluate the whole predicate. Hope I made my self clear. Regarding the apckages, which functions do you refer to as not belong to the base package ? – user3628777 May 14 '14 at 11:51
  • I think question is how to coerce string to logcial. I tried using as.logical(), but it returned NA – user3628777 May 14 '14 at 12:02
  • Maybe what you want is `if(length(predicate.result) > 0 ) ...` . That lets you see if your output essentially exists or not – Carl Witthoft May 14 '14 at 14:24

0 Answers0