Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit variable reference in R

Tags:

r

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.

like image 563
apostolos Avatar asked Jun 22 '11 01:06

apostolos


2 Answers

It may be below:

eval(parse(text=z))
like image 56
Triad sou. Avatar answered Nov 17 '22 00:11

Triad sou.


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))
like image 26
Tommy Avatar answered Nov 17 '22 00:11

Tommy