做什么 .(点)和 %(百分比)在 R 中是什么意思? [英] What do . (dot) and % (percentage) mean in R?

查看:32
本文介绍了做什么 .(点)和 %(百分比)在 R 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题可能听起来很愚蠢,但我注意到 .% 经常在 R 中使用,坦率地说我没有真的知道为什么要使用它.

My question might sound stupid but I have noticed that . and % is often used in R and to be frank I don't really know why it is used.

我在 dplyr 中看到过(去 此处 举例)和data.table(即.SD)但我确定它也必须在其他地方使用.

I have seen it in dplyr (go here for an example) and data.table (i.e. .SD) but I am sure it must be used in other place as well.

因此,我的问题是:

  • . 是什么意思?它是某种 R 编码最佳实践命名法吗?(即 _functionName 常用于 javascript 以表明它是一个私有函数).如果是,规则是什么?
  • % 的相同问题,在 R 中也经常使用(即 %in%,%>%,...).
  • What does . mean? Is it some kind of R coding best practice nomenclature? (i.e. _functionName is often used in javascript to indicate it is a private function). If yes, what's the rule?
  • Same question for %, which is also often used in R (i.e. %in%,%>%,...).

我的猜测一直是 .% 是一种快速调用函数的便捷方式,但是 data.table 使用 . 不遵循这个逻辑,这让我很困惑.

My guess always has been that . and % are a convenient way to quickly call function but the way data.table uses . does not follow this logic, which confuses me.

推荐答案

. 在 R 中没有固有的/神奇的含义.它只是您可以在符号名称中使用的另一个字符.但是因为打字很方便,所以R中的某些函数和约定赋予了它特殊的含义.这里只是一些

. has no inherent/magical meaning in R. It's just another character that you can use in symbol names. But because it is so convenient to type, it has been given special meaning by certain functions and conventions in R. Here are just a few

  • . 用于查找 S3 通用方法实现.例如,如果您使用 lm 类的对象作为第一个参数调用像 plot 这样的泛型函数,那么它将查找名为 plot.lm 的函数,如果找到,就调用它.
  • 通常.在公式中表示所有其他变量",例如lm(y~., data=dd)将回归y在 data.frame dd 中的所有其他变量上.
  • dplyr这样的库使用它作为一个特殊的变量名来指示像do()这样的方法的当前data.frame.他们也可以很容易地选择使用变量名 X 来代替
  • bquote 这样的函数使用 .() 作为特殊函数来转义表达式中的变量
  • 以句点开头的变量被认为是隐藏的"并且不会与 ls() 一起显示,除非您调用 ls(all.names=TRUE)(类似UNIX 文件系统行为)
  • . is used look up S3 generic method implementations. For example, if you call a generic function like plot with an object of class lm as the first parameter, then it will look for a function named plot.lm and, if found, call that.
  • often . in formulas means "all other variables", for example lm(y~., data=dd) will regress y on all the other variables in the data.frame dd.
  • libraries like dplyr use it as a special variable name to indicate the current data.frame for methods like do(). They could just as easily have chosen to use the variable name X instead
  • functions like bquote use .() as a special function to escape variables in expressions
  • variables that start with a period are considered "hidden" and will not show up with ls() unless you call ls(all.names=TRUE) (similar to the UNIX file system behavior)

但是,您也可以定义一个名为 my.awesome.variable<-42 的变量,它会像任何其他变量一样工作.

However, you can also just define a variable named my.awesome.variable<-42 and it will work just like any other variable.

A % 本身并不意味着任何特殊的东西,但是 R 允许您使用两个百分比以 %% 的形式定义自己的中缀运算符迹象.如果你定义

A % by itself doesn't mean anything special, but R allows you to define your own infix operators in the form %<something>% using two percent signs. If you define

`%myfun%` <- function(a,b) {
    a*3-b*2
}

你可以这样称呼它

5 %myfun% 2
# [1] 11

这篇关于做什么 .(点)和 %(百分比)在 R 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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