You can use eval(parse(text=<string>))
, where <string>
is the command you want to execute.
For example (note I have added 'c' to get the syntax right):
gerat1 <- c("DUT$1","16e","1","d")
gerat2 <- c("DUT$7","22e","0.01","d")
gerat3 <- c("DUT$8","32","1","f")
nGerat <- 3 # however many you have
thirds <- rep("", nGerat)
for (ii in 1:nGerat) {
cmd <- sprintf("gerat%d[3]", ii)
thirds[ii] <- eval(parse(text = cmd))
}
but the advice in fortunes(106)
is valid:
If the answer is parse() you should usually rethink the question.
-- Thomas Lumley
R-help (February 2005)
If you can store the original gerat values, however sourced, in a more appropriate manner - e.g. a list - then it will probably be much easier to get what you really need. If instead you can do something like:
gerat <- list()
gerat[[1]] <- c("DUT$1","16e","1","d")
gerat[[2]] <- c("DUT$7","22e","0.01","d")
gerat[[3]] <- c("DUT$8","32","1","f")
thirds <- sapply(gerat, function(g) g[3])
>thirds
[1] "1" "0.01" "1"
thirds2 <- rep("", length(gerat))
for (ii in seq_along(gerat)) {
thirds2[ii] <- gerat[[ii]][3]
}
>thirds2
[1] "1" "0.01" "1"
is a much more robust solution.