-1

I am writing a function where I want to supply a variable which contains a condition to be evaluated inside the function. For example, I have a hourval variable containing values like 0, 3, 6, 9, 18, 3, 6, 9, 18 0, 3, 18 ... I want to select the indices where hourval variable matches to 0, 6. This 0, 6 could change depending upon some other parameters. Basically they are not fixed always. So I pass a variable g1 = call("which", (hourval==0 | hourval == 6)). I want this statement to be evaluated in the program. Hence I use the statement x1 = eval(g1). Obviously, when I pass the variable g1, that time hourval variable is not generated, but it is generated just before the eval(g1) statement. I get error, object hourval not found. Is there any other way to solve this problem.

Thanks in advance, any help is appreciated.

Narayani Barve

rafalotufo
  • 3,862
  • 4
  • 25
  • 28
Narayani
  • 263
  • 1
  • 3
  • 5
  • I think we would get better insight into what you're doing if we would see how you get those parameters. A small working example would go a long way. Here's a little post that can be of some aid: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Jul 12 '11 at 21:07
  • Maybe see the code for `subset.data.frame`; that might help get you started. On the other hand, you may want to think about another way to solve the problem; solutions in the manner you outline are often more trouble than they're worth. – Aaron left Stack Overflow Jul 12 '11 at 21:21
  • please don't cross-post, especially not to an unrelated mailing list: https://stat.ethz.ch/pipermail/r-sig-geo/2011-July/012211.html – mdsumner Jul 13 '11 at 07:03
  • Sort of my fault. I invited Narayani to post his question here (because it's more programming in nature than geostatistical). I should have made it explicit to at least mention a crossposting. – Roman Luštrik Jul 13 '11 at 07:49

2 Answers2

7

Is this what you want?

> hourval <- c(0, 3, 6, 9, 18, 3, 6, 9, 18, 0, 3, 18)
> test <- c(0,6)
> which(hourval %in% test)
[1]  1  3  7 10

It took me a while to find it with this search strategy

library(fortunes)
fortune("parse")

but eventually got the one I remembered:

> fortune("parse")

If the answer is parse() you should usually rethink the question.
   -- Thomas Lumley
      R-help (February 2005)

Part of my difficulty was in the fact that I remembered the quote as having "eval(parse(".

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I see you already posted it in the past. Lumley should be given about 1000 bonus points on SO. – IRTFM Jul 12 '11 at 21:19
  • 1
    Thank you Thomas..The first option works for me. I can send the "test" sequence as a parameter and then use which(hourval %in% test) statement in the place of eval(g1). Thank you very much – Narayani Jul 12 '11 at 21:22
  • I quote that particular fortune fairly regularly at my workplace, due to a coworker who has a fondness for eval/parse. It's an oldie but a goodie. – geoffjentry Jul 13 '11 at 02:03
1

This is what you seem to describe

f1 <- function(y) {
  hourval <- c(0, 3, 6, 9, 18, 3, 6, 9, 18, 0, 3, 18)
  eval(substitute(y))
}
f1( which(hourval %in% c(0,6)) )

But this is what I'd do instead.

f2 <- function(y) {
  hourval <- c(0, 3, 6, 9, 18, 3, 6, 9, 18, 0, 3, 18)
  which(hourval %in% y)
}
f2( c(0,6) )

But again, there's not enough information yet to know if either of these answer the question.

Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142