2016-03-25 68 views
13

我想了解反引號中R.做在R中做些什麼?

從我可以告訴,這不是在?Quotes文檔頁面R.

例如解釋,在R控制檯:

"[[" 
# [1] "[[" 
`[[` 
# .Primitive("[[") 

它似乎返回等同於:

get("[[") 
+0

幫助(「'」)可能是爲了 – hrbrmstr

+1

'help(「\'」)'調出與'?相同的文檔頁面?行情',這是不完全清楚 – Megatron

+0

錯過了在q。道歉。但它對它的作用是一個很好的解釋。 – hrbrmstr

回答

8

通常的方式

temp[[1]] 

提取元一個一對反引號是一種引用名稱或其他保留或非法符號組合的方式。保留字如if是該語言的一部分,而非法包括非句法組合如c a t。這兩個保留和非法的類別在R文檔中被稱爲non-syntactic names

因此,

`c a t` <- 1 # is valid R 

> `+` # is equivalent to typing in a syntactic function name 
function (e1, e2) .Primitive("+") 

如所提到的一個評論者,?Quotes確實包含在反引號的一些信息,Names and Identifiers:

Identifiers consist of a sequence of letters, digits, the period 
(‘.’) and the underscore. They must not start with a digit nor 
underscore, nor with a period followed by a digit. Reserved words 
are not valid identifiers. 

The definition of a _letter_ depends on the current locale, but 
only ASCII digits are considered to be digits.  

Such identifiers are also known as _syntactic names_ and may be 
used directly in R code. Almost always, other names can be used 
provided they are quoted. The preferred quote is the backtick 
(‘`’), and ‘deparse’ will normally use it, but under many 
circumstances single or double quotes can be used (as a character 
constant will often be converted to a name). One place where 
backticks may be essential is to delimit variable names in 
formulae: see ‘formula’. 

這散文下是有點硬解析。這意味着R將一個令牌解析爲一個名稱,它必須是1)一個字母數字序列,週期和下劃線,2)不是該語言中的保留字。否則,要作爲名稱解析,必須使用反引號。

還檢查了?Reserved

Reserved words outside quotes are always parsed to be references 
    to the objects linked to in the ‘Description’, and hence they are 
    not allowed as syntactic names (see ‘make.names’). They *are* 
    allowed as non-syntactic names, e.g. inside backtick quotes. 

此外,先進的R具有的反引號是如何在expressionsenvironmentsfunctions使用的一些例子。

5

它們等同一字不差。例如...試試這個:

df <- data.frame(20a=c(1,2),b=c(3,4)) 

給出錯誤

df <- data.frame(`20a`=c(1,2),b=c(3,4)) 

不給錯誤

3

下面是使用不當的詞彙一個不完整的答案:反引號可以表示與R,你是以非標準方式使用功能。舉例來說,這裏是一個使用[[,列表子集功能:

temp <- list("a"=1:10, "b"=rnorm(5)) 

提取元素之一,使用[[功能

`[[`(temp,1)