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.
It may be below:
eval(parse(text=z))
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With