打破 Richard 的假设,反引号允许您在名称中使用通常不允许的符号。见:
`add+5` <- function(x) {return(x+5)}
定义了一个函数,但是
add+5 <- function(x) {return(x+5)}
返回
Error in add + 5 <- function(x) { : object 'add' not found
要引用该函数,您还需要显式使用反引号。
> `add+5`(3)
[1] 8
要查看此函数的代码,只需在不带参数的情况下调用它:
> `add+5`
function(x) {return(x+5)}
另请参阅此评论,该评论处理名称分配中反引号和引号之间的区别:https://stat.ethz.ch/pipermail/r-help/2006-December/121608.html
注意,反引号的用法更为普遍。例如,在数据框中,您可以将列命名为整数(可能来自对整数因子使用 reshape::cast)。
例如:
test = data.frame(a = "a", b = "b")
names(test) <- c(1,2)
要检索这些列,您可以将反引号与 $ 运算符结合使用,例如:
> test$1
Error: unexpected numeric constant in "test$1"
但是
> test$`1`
[1] a
Levels: a
有趣的是,您不能在分配数据框列名称时使用反引号;以下不起作用:
test = data.frame(`1` = "a", `2` = "b")
响应 statechular 的 cmets,这里还有另外两个用例。
在修复功能中
使用% 符号我们可以天真地定义向量x 和y 之间的点积:
`%.%` <- function(x,y){
sum(x * y)
}
给了
> c(1,2) %.% c(1,2)
[1] 5
更多信息,请参阅:http://dennisphdblog.wordpress.com/2010/09/16/infix-functions-in-r/
替换函数
这是一个很好的答案,展示了这些是什么:What are Replacement Functions in R?