5

How can I evaluate c[,2] through a call to z?

a <- c(1,2,3)
b <- c(4,5,6)
c <- cbind(a,b)
z <- "c[,2]"

eval(z) is not working.

Palec
  • 12,743
  • 8
  • 69
  • 138
apostolos
  • 63
  • 3

2 Answers2

5

It may be below:

eval(parse(text=z))
Marek
  • 49,472
  • 15
  • 99
  • 121
Triad sou.
  • 2,969
  • 3
  • 23
  • 27
3

If you really need to dynamically assemble a function call and then evaluate it, do.call is typically much better (and more efficient). It's a bit hard to pass the missing parameter though, but TRUE also works in this case:

z <- c[TRUE,2]

is equivalent to:

z <- do.call('[', list(c, TRUE, 2))

But here's a hack to get the missing symbol, which can then be used:

m <- quote(f(,))[[2]] # The elusive missing symbol
z <- do.call('[', alist(c, m, 2))
Tommy
  • 39,997
  • 12
  • 90
  • 85