r语言 运算符_R语言运算符

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语言 运算符

你可能感兴趣的:(python,java,机器学习,算法,编程语言)