函数名称用R中的单引号引起来 [英] Function name in single quotation marks in R
问题描述
'row.mean'< - function(mat){
return(apply( mat,1,mean))
}
预先感谢!
在理查德的假设下,后面的勾号允许您使用通常不被允许的名称中的符号。请参阅:
`add + 5`< - function(x){return(x + 5)}
定义了一个函数,但是
add + 5 < - function(x){return(x + 5)}
返回
add + 5错误< - function(x){:object'add'not found
要引用该函数,您还需要明确使用back tick。
> `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)
名称(test)< - c(1,2)
并检索这些列可以使用反向符号与 $
运算符一起使用,例如:
>测试$ 1
错误:test $ 1中出现意外的数字常量
但
>测试$`1`
[1] a
等级:a
Funnily您不能在分配数据框列名称时使用返回勾号;以下不起作用:
test = data.frame(`1` =a,`2` = b)
响应statechular的评论,这里有两个用例。
在修复函数中使用 c>符号我们可以天真地定义向量 给出 替换功能 下面是一个很好的答案,它展示了这些内容:什么是R中的替换函数? It may be a silly question but I have been bothered for quite a while. I've seen people use single quotation marks to surround the function name when they are defining a function. I keep wondering the benefit of doing so. Below is a naive example Thanks in advance! Going off Richard's assumption, the back ticks allows you to use symbols in names which are normally not allowed. See: defines a function, but returns To refer to the function, you need to explicitly use the back ticks as well. To see the code for this function, simply call it without its arguments: See also this comment which deals with the difference between the backtick and quotes in name assignment: https://stat.ethz.ch/pipermail/r-help/2006-December/121608.html Note, the usage of back ticks is much more general. For example, in a data frame you can have columns named with integers (maybe from using For example: and to retrieve these columns you can use the backtick in conjunction with the but Funnily you can't use back ticks in assigning the data frame column names; the following doesn't work: And responding to statechular's comments, here are the two more use cases. In fix functions Using the which gives for more, see: http://dennisphdblog.wordpress.com/2010/09/16/infix-functions-in-r/ Replacement functions Here is a great answer demonstrating what these are: What are Replacement Functions in R? 这篇关于函数名称用R中的单引号引起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!%$ c $
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/
'row.mean' <- function(mat){
return(apply(mat, 1, mean))
}
`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)}
reshape::cast
on integer factors).test = data.frame(a = "a", b = "b")
names(test) <- c(1,2)
$
operator, e.g.:> test$1
Error: unexpected numeric constant in "test$1"
> test$`1`
[1] a
Levels: a
test = data.frame(`1` = "a", `2` = "b")
%
symbol we can naively define the dot product between vectors x
and y
:`%.%` <- function(x,y){
sum(x * y)
}
> c(1,2) %.% c(1,2)
[1] 5