r语言 运算符

R语言中的运算符 (Operators in R Language)

Generally speaking, an operator is a symbol that gives proper commands to the compiler regarding a specific action to be executed. The operators are used for carrying out the mathematical or logical calculations or also sometimes manipulations are also done using these operators. R language is normally considered as the rich language with several built-in operators to make a programmer more comfortable while coding the program. Moving further let us find what all are the inbuilt operators that are offered by the R language to its users.

一般来说,运算符是一个符号,它向编译器提供有关要执行的特定操作的适当命令。 运算符用于执行数学或逻辑计算,或者有时也使用这些运算符进行操作。 R语言通常被认为是具有多种内置运算符的丰富语言,可以使程序员在编写程序时更加舒适。 进一步讲,我们可以找到R语言为其用户提供的所有内置运算符。

Concerning other programming languages like C, C++, Java, etc, here also we find such similar operators that help the coders while coding their program. The commonly used operators are as follows:

关于其他编程语言(例如C,C ++,Java等),在这里我们也找到了类似的运算符,可在编码程序时帮助编码人员。 常用的运算符如下:

  1. Arithmetic operators

    算术运算符

  2. Relational Operators

    关系运算符

  3. Logical Operators

    逻辑运算符

  4. Assignment operators

    赋值运算符

  5. Miscellaneous operators

    杂项运营商

1)算术运算符 (1) Arithmetic Operators)

The operators like +, -, *, /, %%, %/%, ^ fall under the category of the arithmetic operators. The main usage of each operator is explained in details in the following text,

+,-,*,/,%%,%/%,^之类的运算符属于算术运算符的类别。 下文详细说明了每个运算符的主要用法,

Plus Operator (+): This operator helps in adding the two vectors.

加号运算符(+) :此运算符有助于将两个向量相加。

Example:

例:

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v+t)

Output

输出量

[1] 10.0  8.5 10.0

Minus Operator (-): This particular operator subtracts the second vector from the first one.

减号(-) :此特定运算符从第一个向量中减去第二个向量。

Example:

例:

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v-t)

Output

输出量

[1] -6.0  2.5  2.0

Multiply Operator (*): It multiplies both vectors.

乘运算符(*) :将两个向量相乘。

Example:

例:

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v*t)

Output

输出量

[1] 16.0 16.5 24.0

Divide Operator (/): It mainly divides the first vector considered with the second one taken.

除法运算符(/) :主要将考虑的第一个向量除以第二个向量。

Example:

例:

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v/t)

Output

输出量

[1] 0.250000 1.833333 1.500000

Remainder Operator (%%): It gives the remainder of the first vector that is considered with the second one.

余数运算符(%%) :它给出与第二个向量一起考虑的第一个向量的余数。

Example:

例:

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%%t)

Output

输出量

[1] 2.0 2.5 2.0

Division Operator (%/%): The result of the division of the first vector to the second one(quotient).

除法运算符(%/%) :第一个向量除以第二个向量(商)的结果。

Example:

例:

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%/%t)

Output

输出量

[1] 0 1 1

Raised to the Power (^): The first vector raised to the second one.

提高到幂(^) :第一个矢量提高到第二个。

Example:

例:

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v^t)

Output

输出量

[1]  256.000  166.375 1296.000

2)关系运算符 (2) Relational Operators)

This is one of the sub-branches under the operators available in the R language. The main use of these operators is to find the relation between the two vectors considered.

这是R语言中可用的运算符的子分支之一。 这些运算符的主要用途是找到所考虑的两个向量之间的关系。

The result of these operators is usually a boolean value.

这些运算符的结果通常是布尔值。

Coming to the list of available relational operators in the R language are as follows:

R语言中可用的关系运算符列表如下:

Greater Than >
Less Than <
Equal To ==
Greater Than or Equal To >=
Less Than or Equal To <=
Not Equal To !=
比...更棒 >
少于 <
等于 ==
大于或等于 > =
小于或等于 <=
不等于 !=

The above operators have their own meanings while executing the program.

上述运算符在执行程序时具有各自的含义。

Greater Than Operator (>)

大于运算符(>)

The above operator helps the user in the process of determining whether the first number is greater than the second one.

上述运算符在确定第一数字是否大于第二数字的过程中帮助用户。

Example:

例:

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>t)

Output

输出量

[1] FALSE  TRUE FALSE FALSE

Less Than Operator (

少于运算符(

This operator tells the user whether the first operator is less than the second one.

该运算符告诉用户第一个运算符是否小于第二个运算符。

Example:

例:

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v < t)

Output

输出量

[1]  TRUE FALSE  TRUE FALSE

Equal To Operator (==)

等于运算符(==)

Finds whether the both values are same to each other or not.

查找两个值是否彼此相同。

Example:

例:

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v == t)

Output

输出量

[1] FALSE FALSE FALSE  TRUE

Greater Than or Equal To Operator (>=)

大于或等于运算符(> =)

Confirms whether each component of the foremost vector is superior than or equivalent to the corresponding part of the subsequent vector.

确认最前向量的每个分量是否优于或等效于后续向量的相应部分。

Example:

例:

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>=t)

Output

输出量

[1] FALSE  TRUE FALSE  TRUE

Less Than or Equal To Operator (<=)

小于或等于运算符(<=)

Verifies if each component of the initial vector is less than or equal to the equivalent element of the next vector.

验证初始向量的每个分量是否小于或等于下一个向量的等效元素。

Example:

例:

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v<=t)

Output

输出量

[1]  TRUE FALSE  TRUE  TRUE

Not Equal To Operator (!=)

不等于运算符(!=)

Verifies if each component of the initial vector is not equal to the equivalent element of the following vector.

验证初始向量的每个分量是否不等于后续向量的等效元素。

Example:

例:

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v!=t)

Output

输出量

[1]  TRUE  TRUE  TRUE FALSE

3)逻辑运算符 (3) Logical Operators)

The following table shows all the available logical operators in the R language. The below operators are applicable over the vectors which are complex, logical or numeric-only. Here the R compiled assumes that all the numbers that are greater than the one are assumed to possess a logic value true.

下表显示了R语言中所有可用的逻辑运算符。 以下运算符适用于复杂,仅逻辑或仅数字的向量。 在此,R编译器假定所有大于一个的数字均具有逻辑值true。

Here while the logical operators are considered then every element available in the first vector is usually compared with the corresponding next element of the second vector. Thus, the result of the above comparison gives us a Boolean value.

在这里,在考虑逻辑运算符的同时,通常将第一向量中的每个可用元素与第二向量中的对应的下一个元素进行比较。 因此,以上比较的结果给出了一个布尔值。

The logical operators offered by the R language are as follows:

R语言提供的逻辑运算符如下:

Element-wise Logical AND operator &
Element-wise Logical OR operator |
Logical NOT operator !
Logical AND operator &&
Logical OR operator ||
逐元素逻辑AND运算符
按元素逻辑或运算符 |
逻辑非运算符
逻辑AND运算子 &&
逻辑或运算符 ||

Element-wise Logical AND operator (&)

逐元素逻辑AND运算符(&)

It is named as the Element-wise Logical AND operator. It combines each component of the primary vector with the equivalent part of the next vector and gives an output TRUE if both the fundamental aspects are TRUE.

它被称为Element-wise Logical AND运算符 。 如果两个基本方面都为TRUE,它将主要向量的每个分量与下一个向量的等效部分合并,并给出输出TRUE。

Example:

例:

v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
print(v&t)

Output

输出量

[1]  TRUE  TRUE FALSE  TRUE

Element-wise Logical OR operator (|)

逐元素逻辑或运算符(|)

It is termed as Element-wise Logical OR operator. It combines each element of the primary vector with the matching element of the subsequent vector and gives an output TRUE if one of the elements is TRUE.

它被称为元素智能逻辑或运算符 。 它将主向量的每个元素与后续向量的匹配元素组合在一起,如果其中一个元素为TRUE,则输出为TRUE。

Example:

例:

v <- c(3,0,TRUE,2+2i)
t <- c(4,0,FALSE,2+3i)
print(v|t)

Output

输出量

[1]  TRUE FALSE  TRUE  TRUE

Logical NOT operator (!)

逻辑非运算符(!)

It is usually called with the name Logical NOT operator. The main function of this operator is that it takes each component of the vector and gives the contradictory logical value.

通常使用名称Logical NOT运算符进行调用。 该运算符的主要功能是获取矢量的每个分量并给出矛盾的逻辑值。

Example:

例:

v <- c(3,0,TRUE,2+2i)
print(!v)

Output

输出量

[1] FALSE  TRUE FALSE FALSE

Logical AND operator (&&)

逻辑AND运算符(&&)

Named as Logical AND operator. It takes the initial component of the first vector and compares it with the second one and finally gives the TRUE value if and only if both are TRUE.

命名为逻辑AND运算符 。 它采用第一个向量的初始分量,并将其与第二个向量进行比较,并且当且仅当两者均为TRUE时,才给出TRUE值。

Example:

例:

v <- c(3,0,TRUE,2+2i)
t <- c(1,3,TRUE,2+3i)
print(v&&t)

Output

输出量

[1] TRUE

Logical OR operator (||)

逻辑或运算符(||)

It is termed as the Logical OR operator. The function of this operator is that it considers the primary components of both the vectors and eventually returns the value as TRUE if and only if one of them turns out to be TRUE.

它被称为逻辑或运算符 。 该运算符的功能是,它考虑两个向量的主要成分,并且仅当其中一个结果为TRUE时,才最终将值返回TRUE。

Example:

例:

v <- c(0,0,TRUE,2+2i)
t <- c(0,3,TRUE,2+3i)
print(v||t)

Output

输出量

[1] FALSE

4)赋值运算符 (4) Assignment Operators)

The main intention of using these assignment operators in the program is to assign some value to the variables or vectors declared.

在程序中使用这些赋值运算符的主要目的是为声明的变量或向量赋值。

There are two ways while assigning the vectors is considered in the R language. They are:

在R语言中,可以采用两种方式分配向量。 他们是:

  • Right assignment

    正确的分配

  • Left assignment

    左分配

Right assignment:

正确分配:

The operators used for right assignment are as follows:

用于权限分配的运算符如下:

  • =

    =

  • <

    <

Example:

例:

v1 <- c(3,1,TRUE,2+3i)
v2 <<- c(3,1,TRUE,2+3i)
v3 = c(3,1,TRUE,2+3i)
print(v1)
print(v2)
print(v3)

Output

输出量

[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i

Left assignment:

左分配:

Similarly, the operators used while implementing the left assignment technique is as follows:

同样,实现左分配技术时使用的运算符如下:

  • ->

    ->

  • ->>

    ->>

Example:

例:

c(3,1,TRUE,2+3i) -> v1
c(3,1,TRUE,2+3i) ->> v2
print(v1)
print(v2)

Output

输出量

[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i

5)杂项运营商 (5) Miscellaneous Operators)

These miscellaneous operators have their own specific purpose of usage in the program. They do not deal with either the logical or mathematical calculations or computations.

这些其他运算符在程序中有其特定的使用目的。 它们不处理逻辑或数学计算或计算。

Operator Description Example
: Colon operator: This operator creates the series of numerical in sequence for a considered vector.
v <- 2:8
print(v)

Output

%in% This operator is mainly used to recognize if an element/component belongs to the particular vector that is considered.
v1 <- 8
v2 <- 12
t <- 1:10
print(v1 %in% t)
print(v2 %in% t)

Output

%*% This particular operator is used in the case when the multiplication of a matrix with its transpose is required to be performed.
M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE)
t = M %*% t(M)
print(t)

Output

操作员 描述
冒号运算符 :此运算符为考虑的向量顺序创建一系列数值。
v < - 2 : 8
print ( v )

输出量

%在% 该运算符主要用于识别元素/组件是否属于所考虑的特定向量。
v1 < - 8
v2 < - 12
t < - 1 : 10
print ( v1 % in % t )
print ( v2 % in % t )

输出量

%*% 在需要执行矩阵与其转置相乘的情况下,使用此特定运算符。
M = matrix ( c ( 2 , 6 , 5 , 1 , 10 , 4 ) , nrow = 2 , ncol = 3 , byrow = TRUE )
t = M %*% t(M)
print(t)

输出量

翻译自: https://www.includehelp.com/r/operators.aspx

r语言 运算符

r语言 运算符_R语言运算符相关推荐

  1. r语言简介_R语言简介

    r语言简介 R is a language and environment for statistical computing and graphics that is supported by th ...

  2. pvrect r语言 聚类_R语言一条命令实现基于样本和距离的聚类分析

    上一篇文章给大家介绍了利用 R语言的 hclust()进行聚类分析的步骤,已经很简单了,但是依然有不少小伙伴来问 "老师,还有更简单的方法吗,最好是一条命令那种",为了满足的大家的 ...

  3. 找不到r低版本_R 语言与数据挖掘直播班开始招生,生信分析帮你发高分文章

    数据单薄很难支撑文章内容?数据有了不知道怎么处理作出高级的图片?这个时候需要的是生信分析--深度的数据挖掘和分析处理,可以帮助临床医生通过数据处理得到自己想要的信息,更快速地发文章. 学习哪种生信分析 ...

  4. r语言 中断r的输入_R语言_004数据输入

    现实的情况是,我们大部分遇到的都是表格数据,在R语言里面叫数据框,数据来源一般不可能我们自己在程序开始前手动录入,正常的逻辑是从外面读取现成的数据,再预处理.建模什么的.根据经验,现在的数据来源主要是 ...

  5. r语言散点图_R语言 | 散点图入门:以声学元音图为例

    R语言 语言学与R语言的碰撞 Xu & Yang PhoneticSan 学习参考 Discovering Statistics Using R Statistics for Linguist ...

  6. mcem r语言代码_R语言阈值自回归模型(TAR)代码示例

    原文链接: R语言时间序列TAR阈值模型分析​tecdat.cn 阈值模型用于统计的几个不同区域,而不仅仅是时间序列.一般的想法是,当变量的值超过某个阈值时,过程可能表现不同.也就是说,当值大于阈值时 ...

  7. pvrect r语言 聚类_R语言常用统计方法包+机器学习包(名称、简介)

    上期帮大家盘点了一下R中常用的可视化包,这期将简要盘点一下关于统计分析与机器学习的R包,并通过简要介绍包的特点来帮助读者深入理解可视化包. 本文作者为"食物链顶端"学习群中的小伙伴 ...

  8. r语言回归分析_R语言之cox回归分析

    Cox比例风险模型(Cox proportional-hazards model,也称为Cox回归),主要用于带有时间的生存结局的影响因素研究,或评价某个临床治疗措施对患者生存的影响. Cox模型可以 ...

  9. r计算百分位数_R语言:用R计算各种统计值

    目录:求极差(range) 做频数分布表和频数分布图(graph of frequency distribution) 算术平均数(mean) 几何均数(geometric mean) 中位数与百分位 ...

最新文章

  1. hdu 1789 Doing Homework again
  2. 《疯狂Java讲义》9
  3. 【数据结构与算法】之深入解析“游乐园的迷宫”的求解思路与算法示例
  4. RoboGuice入门
  5. Python 函数装饰器
  6. 钱币掉落动画android,mpvue实现小程序签到金币掉落动画(api实现)
  7. ACL20 | 让笨重的BERT问答匹配模型变快!
  8. Win32中如何判断多个键同时按下
  9. 使用jQuery来实现一个简单的ajax请求
  10. 网站备案负责人_三分钟教你如何做好网站备案
  11. python 托盘_[宜配屋]听图阁
  12. 开源啦:连DeepMind也捉急的游戏,OpenAI给你攻破第一关的高分算法
  13. JS动态呈现还可以输入字数
  14. 微信公众请求config php,微信公众平台开发之配置与请求,微信公众平台
  15. 火狐浏览器配置webDriver
  16. Asterisk 入门教程
  17. 成功解决 XXX--1.0-SNAPSHOT.jar中没有主清单属性
  18. 伊甸园日历游戏 飘飘乎居士拯救MM(tyvj 1968 1140)
  19. 曲卉:高阶增长黑客实战营
  20. 计算机系统实验--BombLab

热门文章

  1. so把asp页面生成静态的html,23、asp系列课程--server.URLEncode方法和server.HTMLEncode方法...
  2. 共享内存简介及docker容器的shm设置与修改
  3. Linux命令行性能监控工具大全
  4. linux下调用python脚本,Linux下QT调用Python脚本的解决方案,Qt,python,一种,解决办法
  5. 用udp协议通讯时怎样得知目标机是否获得了数据包?_和相亲对象聊天,你属于UDP还是CDP?...
  6. suse 安装oracle11,Suse11安装Oracle11gR2
  7. 雷神开机logo更改_国产外星人雷神再发新品 911MT逐影者RTX2060光追游戏本评测
  8. 计算机有什么著名基金经理排名,百万年薪的基金经理,都是什么专业出身?!...
  9. HTML多选mysql,html多选下拉框 | 学步园
  10. css黑色字白色描边,css怎么设置字体白色描边