R:来自值序列的取代表达式的序列

时间:2015-05-14 15:46:19

标签: r expression quote

我想制作

list(quote(10^2), quote(10^3), quote(10^4),
     quote(10^5), quote(10^6), quote(10^7))

seq(2,7)

是否有一种不那么笨拙的方式来做到这一点

mapply(function(n) substitute(10^x, list(x=as.double(n))), seq(2,7))

?我尝试过以下方法:

> 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]])

2 个答案:

答案 0 :(得分:7)

尝试bquote

lapply(as.numeric(2:7), function(x) bquote(10^.(x)))

答案 1 :(得分:2)

你应该可以这样做:

lapply(2:7, function(x) {
  substitute(10^x, list(x = x))
})

示例:

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

唯一的区别是我的版本将值2:7视为整数(因此为“L”)。