R:像%in%这样的运算符叫什么,我如何了解它们? [英] R: What are operators like %in% called and how can I learn about them?

查看:261
本文介绍了R:像%in%这样的运算符叫什么,我如何了解它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道像==!=这样的基础知识,甚至(模糊地)在&&&之间的区别.但是像%in%%%这样的东西,以及在sprintf()上下文中使用的一些东西,例如sprintf("%.2f", x)的东西,我都不知道.

I know the basics like == and !=, or even the difference (vaguely) between & and &&. But stuff like %in% and %% and some stuff used in the context of sprintf(), like sprintf("%.2f", x) stuff I have no idea about.

最糟糕的是,它们很难在Internet上搜索,因为它们是特殊字符,我不知道它们的名字...

Worst of all, they're hard to search for on the Internet because they're special characters and I don't know what they're called...

推荐答案

这里发生了几处带有百分比符号的不同事情:

There are several different things going on here with the percent symbol:

正如一些人已经指出的那样,形式为%%%in%%*%的东西都是二进制运算符(分别取模,匹配和矩阵乘法),就像+-,它们是对两个参数进行操作的函数,由于其名称结构(以%开头和结尾),R认为这两个参数是特殊的.这使您可以以以下形式使用它们:

As several have already pointed out, things of the form %%, %in%, %*% are binary operators (respectively modulo, match, and matrix multiply), just like a +, -, etc. They are functions that operate on two arguments that R recognizes as being special due to their name structure (starts and ends with a %). This allows you to use them in form:

Argument1 %fun_name% Argument2

代替传统的:

fun_name(Argument1, Argument2)

请记住,以下是等效的:

Keep in mind that the following are equivalent:

10 %% 2 == `%%`(10, 2)
"hello" %in% c("hello", "world") == `%in%`("hello", c("hello", "world"))
10 + 2 == `+`(10, 2)

R仅将标准运算符和%x%运算符识别为特殊运算符,并且如果不引用它们,则允许您将它们用作传统的二进制运算符.如果您引用它们(在上面带反引号的示例中),则可以将它们用作标准的两个参数函数.

R just recognizes the standard operators as well as the %x% operators as special and allows you to use them as traditional binary operators if you don't quote them. If you quote them (in the examples above with backticks), you can use them as standard two argument functions.

标准二进制运算符和%x%运算符之间的最大区别是,您可以定义自定义二进制运算符,而R会将其识别为特殊并将其视为二进制运算符:

The big difference between the standard binary operators and %x% operators is that you can define custom binary operators and R will recognize them as special and treat them as binary operators:

`%samp%` <- function(e1, e2) sample(e1, e2)
1:10 %samp% 2
# [1] 1 9

在这里,我们定义了示例函数的二进制运算符版本

Here we defined a binary operator version of the sample function

sprintfformat之类的函数中,"%"的含义完全不同,与二进制运算符无关.需要注意的关键是在这些函数中,%字符是带引号的字符串的一部分,而不是命令行上的标准符号(即"%"%有很大不同).在sprintf的上下文中,在字符串内,"%"是一个特殊字符,用于识别后续字符具有特殊含义,不应将其解释为常规文本.例如,在:

The meaning of "%" in function like sprintf or format is completely different and has nothing to do with binary operators. The key thing to note is that in those functions the % character is part of a quoted string, and not a standard symbol on the command line (i.e. "%" and % are very different). In the context of sprintf, inside a string, "%" is a special character used to recognize that the subsequent characters have a special meaning and should not be interpreted as regular text. For example, in:

sprintf("I'm a number: %.2f", runif(3))
# [1] "I'm a number: 0.96" "I'm a number: 0.74" "I'm a number: 0.99"

"%.2f" 表示要显示的浮点数( f ),带有两个小数位( .2 ).注意"I'm a number: "片段是如何按字面解释的. "%"的使用允许sprintf用户将文字文本与有关如何表示其他sprintf自变量的特殊说明混合在一起.

"%.2f" means a floating point number (f) to be displayed with two decimals (.2). Notice how the "I'm a number: " piece is interpreted literally. The use of "%" allows sprintf users to mix literal text with special instructions on how to represent the other sprintf arguments.

这篇关于R:像%in%这样的运算符叫什么,我如何了解它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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