函数名称用R中的单引号引起来 [英] Function name in single quotation marks in R

查看:184
本文介绍了函数名称用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>符号我们可以天真地定义向量 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/



替换功能



下面是一个很好的答案,它展示了这些内容:什么是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

'row.mean' <- function(mat){
    return(apply(mat, 1, mean))
}

Thanks in advance!

解决方案

Going off Richard's assumption, the back ticks allows you to use symbols in names which are normally not allowed. See:

`add+5` <- function(x) {return(x+5)}

defines a function, but

add+5 <-  function(x) {return(x+5)}

returns

Error in add + 5 <- function(x) { : object 'add' not found

To refer to the function, you need to explicitly use the back ticks as well.

> `add+5`(3)
[1] 8

To see the code for this function, simply call it without its arguments:

> `add+5`
function(x) {return(x+5)}

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 reshape::cast on integer factors).

For example:

test = data.frame(a = "a", b = "b")
names(test) <- c(1,2)

and to retrieve these columns you can use the backtick in conjunction with the $ operator, e.g.:

> test$1
Error: unexpected numeric constant in "test$1"

but

> test$`1`
[1] a
Levels: a

Funnily you can't use back ticks in assigning the data frame column names; the following doesn't work:

test = data.frame(`1` = "a", `2` = "b")

And responding to statechular's comments, here are the two more use cases.

In fix functions

Using the % symbol we can naively define the dot product between vectors x and y:

`%.%` <- function(x,y){

    sum(x * y)

}

which gives

> c(1,2) %.% c(1,2)
[1] 5

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆