简体   繁体   English

R变量名称:符号/名称或其他东西?

[英]R variable names: symbols/names or something else?

Reading through the official R documentation as well as some of the contributed tutorials one learns that variable names are regarded as language objects - ie they are symbols aka names. 通过阅读官方R文档以及一些贡献的教程,我们可以了解到变量名称被视为语言对象 - 即它们是符号即名称。

On p. 在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" . Symbol LookUp标题下的R语言定义手册(版本3.1.1)中的14个是一个简单的例子: "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). 令人困惑的是is.symbol(y) ,或等效的是is.name(y) ,返回FALSE (对于引用和不带引号的参数y)。 When one coerces the variable into a symbol with y <- as.name(4) , then is.symbol(y) and is.name(y) return TRUE . 当一个人将变量y <- as.name(4)转换为y <- as.name(4)的符号时,则is.symbol(y)is.name(y)返回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? 什么类型的R对象在被强制转换为符号之前是变量名?

Thanks for your help in clearing up this confusion. 感谢您帮助我解决这一困惑。

It's important to understand what is.symbol and is.name are doing. 理解什么是is.symbolis.name正在做的很重要。 First, they are really the same function. 首先,它们实际上是相同的功能。 Observe that 观察那个

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. 尽管foo本身就是一个名字,但它所指出的还没有定义。 It's trying to "look-up" the value of foo . 它试图“查找” foo的价值。 Observe that 观察那个

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. 因此, quote会将参数视为语言对象,您可以通过这种方式对其进行测试。 Now if we define foo to point to a name 现在,如果我们定义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. 因此变量名称是名称/符号,但通常大多数R函数将与它们指向的内容一起工作,而不是返回有关名称本身的信息。 So the problem was you were misinterpreting how is.name and is.symbol were working. 所以问题是你误解了is.nameis.symbol是如何工作的。 It only really makes a difference when you are programming on the language. 当您使用该语言编程时,它才真正有所作为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM