gpt4 book ai didi

r - R中引号和表达式之间的区别

转载 作者:行者123 更新时间:2023-12-04 10:17:49 28 4
gpt4 key购买 nike

gettypes = function(x) {paramx = substitute(x); print(typeof(paramx)); print(class(paramx)); print(mode(paramx))}
gettypes(expression(sin(x+y)))
# [1] "language"
# [1] "call"
# [1] "call"
gettypes(quote(sin(x+y)))
# [1] "language"
# [1] "call"
# [1] "call"
expression(sin(x+y)) == quote(sin(x+y))

Error in expression(sin(x + y)) == quote(sin(x + y)) : comparison is not allowed for expressions


identical(expression(sin(x+y)), quote(sin(x+y)))
# [1] FALSE
x = 1
y = 2
eval(expression(sin(x+y)))
# [1] 0.14112
eval(quote(sin(x+y)))
# [1] 0.14112

它们看起来几乎一样。

最佳答案

  • expression将其参数作为未求值表达式的向量返回。
  • quote以未计算的表达式形式返回其参数。

  • 试试这个:
    (e1 <- quote(sin(x+y)))
    # sin(x + y)
    (e2 <- expression(sin(x+y)))
    # expression(sin(x + y))
    str(e1)
    # language sin(x + y)
    str(e2)
    # expression(sin(x + y))
    str(e2[[1]])
    # language sin(x + y)
    all.equal(e1, e2)
    # [1] "Modes of target, current: call, expression" "target, current do not match when deparsed"
    all.equal(e1, e2[[1]])
    # [1] TRUE

    另一个例子:
    e2 = expression(sin(x+y), x+y)
    e2
    # expression(sin(x + y), x + y)
    e2[1]
    # expression(sin(x + y))
    e2[2]
    # expression(x + y)

    关于r - R中引号和表达式之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26531615/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com