Reading through the official R documentation as well as some of the contributed tutorials one learns that variable names are regarded as language objects - i.e. they are symbols aka names.
通过官方R文档以及一些贡献的教程阅读,我们了解到变量名称被视为语言对象 - 即它们是符号,即名称。
On p. 14 of the R Language Definition manual (version 3.1.1) under the heading of Symbol LookUp is a simple example: "y <- 4 ... y is a symbol and 4 is its value"
. What is confusing about this is that is.symbol(y)
, or equivalently is.name(y)
, return FALSE
(for quoted and unquoted argument y). When one coerces the variable into a symbol with y <- as.name(4)
, then is.symbol(y)
and is.name(y)
return TRUE
. So it seems that variable names are not symbols/names until they are coerced into such. What kind of R object is a variable name before it is coerced into a symbol?
在p。 Symbol LookUp标题下的R语言定义手册(版本3.1.1)中的14个是一个简单的例子:“y < - 4 ... y是一个符号,4是它的值”。令人困惑的是is.symbol(y),或等效的是.name(y),返回FALSE(对于引用和不带引号的参数y)。当一个人将变量强制转换为y < - as.name(4)的符号时,则is.symbol(y)和is.name(y)返回TRUE。因此,变量名称似乎不是符号/名称,直到它们被强制转换为符号/名称。什么类型的R对象在被强制转换为符号之前是变量名?
Thanks for your help in clearing up this confusion.
感谢您帮助我解决这一困惑。
1 个解决方案
#1
11
It's important to understand what is.symbol
and is.name
are doing. First, they are really the same function. Observe that
了解is.symbol和is.name正在做什么很重要。首先,它们实际上是相同的功能。观察那个
is.symbol
# function (x) .Primitive("is.symbol")
is.name
# function (x) .Primitive("is.symbol")
so symbols/names are really the same thing in R. So here I will just use is.name
因此符号/名称在R中实际上是相同的。所以在这里我将使用is.name
But also note that these functions are checking if the "thing" that the name you pass in points to is a symbol or a name. They are looking up what the name points to.
但另请注意,这些函数正在检查您传递的名称所指的“东西”是符号还是名称。他们正在查找名称所指的内容。
So if you did
所以如果你这样做了
# rm(foo) make sure it doesn't exist
is.name(foo)
# Error: object 'foo' not found
you get an error. Despite the fact that foo
is a name itself, what it points to is not yet defined. It's trying to "look-up" the value of foo
. Observe that
你收到一个错误。尽管foo本身就是一个名字,但它所指出的还没有定义。它试图“查找”foo的价值。观察那个
quote(foo)
# foo
is.name(quote(foo))
# [1] TRUE
So quote
will treat the parameter like a language object and you can test it that way. Now if we define foo
to point to a name
因此,quote会将参数视为语言对象,您可以通过这种方式对其进行测试。现在,如果我们定义foo指向一个名称
(foo <- as.name("hello"))
# hello
is.name(foo)
# [1] TRUE
but if we point it to something else
但如果我们把它指向别的东西
(foo <- "hello")
# [1] "hello"
is.name(foo)
# [1] FALSE
is.character(foo)
# [1] TRUE
then it is no longer pointing to a name (here, it points to a character)
然后它不再指向一个名称(这里,它指向一个字符)
So variable names are names/symbols, but generally most R function will work with what they point to rather than return information about the name itself. So the problem was you were misinterpreting how is.name
and is.symbol
were working. It only really makes a difference when you are programming on the language.
因此变量名称是名称/符号,但通常大多数R函数将与它们指向的内容一起工作,而不是返回有关名称本身的信息。所以问题是你误解了is.name和is.symbol是如何工作的。当你在语言上编程时,它才真正有所作为。
#1
11
It's important to understand what is.symbol
and is.name
are doing. First, they are really the same function. Observe that
了解is.symbol和is.name正在做什么很重要。首先,它们实际上是相同的功能。观察那个
is.symbol
# function (x) .Primitive("is.symbol")
is.name
# function (x) .Primitive("is.symbol")
so symbols/names are really the same thing in R. So here I will just use is.name
因此符号/名称在R中实际上是相同的。所以在这里我将使用is.name
But also note that these functions are checking if the "thing" that the name you pass in points to is a symbol or a name. They are looking up what the name points to.
但另请注意,这些函数正在检查您传递的名称所指的“东西”是符号还是名称。他们正在查找名称所指的内容。
So if you did
所以如果你这样做了
# rm(foo) make sure it doesn't exist
is.name(foo)
# Error: object 'foo' not found
you get an error. Despite the fact that foo
is a name itself, what it points to is not yet defined. It's trying to "look-up" the value of foo
. Observe that
你收到一个错误。尽管foo本身就是一个名字,但它所指出的还没有定义。它试图“查找”foo的价值。观察那个
quote(foo)
# foo
is.name(quote(foo))
# [1] TRUE
So quote
will treat the parameter like a language object and you can test it that way. Now if we define foo
to point to a name
因此,quote会将参数视为语言对象,您可以通过这种方式对其进行测试。现在,如果我们定义foo指向一个名称
(foo <- as.name("hello"))
# hello
is.name(foo)
# [1] TRUE
but if we point it to something else
但如果我们把它指向别的东西
(foo <- "hello")
# [1] "hello"
is.name(foo)
# [1] FALSE
is.character(foo)
# [1] TRUE
then it is no longer pointing to a name (here, it points to a character)
然后它不再指向一个名称(这里,它指向一个字符)
So variable names are names/symbols, but generally most R function will work with what they point to rather than return information about the name itself. So the problem was you were misinterpreting how is.name
and is.symbol
were working. It only really makes a difference when you are programming on the language.
因此变量名称是名称/符号,但通常大多数R函数将与它们指向的内容一起工作,而不是返回有关名称本身的信息。所以问题是你误解了is.name和is.symbol是如何工作的。当你在语言上编程时,它才真正有所作为。