R: a sequence of substituted expressions from a sequence of values
I want to create
list(quote(10^2), quote(10^3), quote(10^4),
quote(10^5), quote(10^6), quote(10^7))
from
seq(2,7)
is there a less fluid way to do this than
mapply(function(n) substitute(10^x, list(x=as.double(n))), seq(2,7))
? I've tried the following:
> substitute(10^x, list(x=seq(2,7)))
10^2:7
> mapply(substitute, 10^x, x=seq(2,7))
Error in mapply(substitute, 10^x, x = seq(2, 7)) : object 'x' not found
> mapply(function(n) substitute(10^n), seq(2,7))
list(10^dots[[1L]][[6L]], 10^dots[[1L]][[6L]], 10^dots[[1L]][[6L]],
10^dots[[1L]][[6L]], 10^dots[[1L]][[6L]], 10^dots[[1L]][[6L]])
+3
source to share
2 answers
You should be able to:
lapply(2:7, function(x) {
substitute(10^x, list(x = x))
})
Example:
test <- lapply(2:7, function(x) {
substitute(10^x, list(x = x))
})
str(test)
# List of 6
# $ : language 10^2L
# $ : language 10^3L
# $ : language 10^4L
# $ : language 10^5L
# $ : language 10^6L
# $ : language 10^7L
orig <- list(quote(10^2), quote(10^3), quote(10^4),
quote(10^5), quote(10^6), quote(10^7))
str(orig)
# List of 6
# $ : language 10^2
# $ : language 10^3
# $ : language 10^4
# $ : language 10^5
# $ : language 10^6
# $ : language 10^7
The only difference is that my version treats 2: 7 values as integers (hence the "L").
+2
source to share