English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

R Basic Operations

This chapter introduces simple operations in the R language.

Assignment

Generally, assignment in programming languages is = symbol, but since R is a mathematical language, the assignment symbol is very similar to the pseudocode in our mathematical books, which is a left arrow <- :

a <- 123
b <- 456
print(a + b)

The result of executing the above code:

[1] 579

This assignment symbol is a formal advantage and an operational disadvantage of the R language: formally more suitable for mathematicians, after all, not all mathematicians are accustomed to using = as an assignment symbol.

In terms of operation, the < symbol and - The symbols are not very easy to type characters, which will make many programmers feel uncomfortable. Therefore, the relatively new version of the R language also supports = as an assignment operator:

a = 123
b = 456
print(a + b)

This is also a valid R program.

Note:It is difficult to verify from which version of R support was added = Assignment, but the R version used in this tutorial is 4.0.0.

Mathematical operators

The table below lists the main mathematical operators and their order of operations:

PrioritySymbolsMeaning
1()Parentheses
2^Power operation
3%%Integer division remainder
 %/%Integer division
4*Multiplication
 /Division
5+Addition
 -Subtraction

The following examples demonstrate simple mathematical operations:

> 1 + 2 * 3
[1] 7
> (1 + 2) * 3
[1] 9
> 3 / 4
[1] 0.75
> 3.4 - 1.2
[1] 2.2
> 1 - 4 * 0.5^3
[1] 0.5
> 8 / 3 %% 2
[1] 8
> 8 / 4 %% 2
[1Inf
> 3 %% 2^2
[1] 3
> 10 / 3 %/% 2
[1] 10

Relational operators

The following table lists the relational operators supported by R language, which compare two vectors, compare each element of the first vector with the corresponding element of the second vector, and return a boolean value as the result.

OperatorDescription
>Judges whether each element of the first vector is greater than the corresponding element of the second vector.
<Judges whether each element of the first vector is less than the corresponding element of the second vector.
==Judges whether each element of the first vector is equal to the corresponding element of the second vector.
!=Judges whether each element of the first vector is not equal to the corresponding element of the second vector.
>=Judges whether each element of the first vector is greater than or equal to the corresponding element of the second vector.
<=Judges whether each element of the first vector is less than or equal to the corresponding element of the second vector.
v <- c(2,4,6,9)
t <- c(1,4,7,9)
print(v>t)
print(v < t)
print(v == t)
print(v!=t)
print(v>=t)
print(v<=t)

The output of the above code is:

[1TRUE FALSE FALSE FALSE
[1FALSE FALSE  TRUE FALSE
[1FALSE TRUE FALSE TRUE
[1TRUE FALSE  TRUE FALSE
[1]  TRUE  TRUE FALSE  TRUE
[1FALSE  TRUE  TRUE  TRUE

Logical operators

The following table lists the logical operators supported by R language, which can be used for vectors of numeric, logical, and complex types.

greater than 1 numbers are all TRUE.

Logical operators compare two vectors, comparing each element of the first vector with the corresponding element of the second vector, and returning a boolean value as the result.

OperatorDescription
&Element logical AND operator, combines each element of the first vector with the corresponding element of the second vector, if both elements are TRUE, the result is TRUE, otherwise FALSE.
Element logical OR operator, combines each element of the first vector with the corresponding element of the second vector, if one of the two elements is TRUE, the result is TRUE, if both are FALSE, it returns FALSE.
!Logical NOT operator, returns the opposite logical value of each element in the vector, if the element is TRUE, it returns FALSE, and if the element is FALSE, it returns TRUE.
&&Logical AND operator, it only judges the first element of two vectors, if both elements are TRUE, the result is TRUE, otherwise FALSE.
||The logical OR operator only judges the first element of the two vectors, if one of the two elements is TRUE, the result is TRUE, and if both are FALSE, it returns FALSE.
v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
print(v&t)
print(v|t)
print(!v)
# &&、|| only compare the first element of the two vectors
v <- c(3,0,TRUE,2+2i)
t <- c(1,3,TRUE,2+3i)
print(v&&t)
v <- c(0,0,TRUE,2+2i)
t <- c(0,3,TRUE,2+3i)
print(v||t)

The output of the above code is:

[1]  TRUE  TRUE FALSE  TRUE
[1] TRUE TRUE TRUE TRUE
[1] FALSE FALSE FALSE FALSE
[1] TRUE
[1] FALSE

Assignment operators

R language variables can use left, right, or equal operators to assign values.

The table below lists the assignment operators supported by R language.

OperatorDescription

<−

=

<<−

Left assignment.

−>

−>>

Right assignment.
# Left assignment
v1 <- c(3,1,TRUE,"w3codebox")
v2 <<- c(3,1,TRUE,"w3codebox")
v3 = c(3,1,TRUE,"w3codebox")
print(v1)
print(v2)
print(v3)
# Right assignment
c(3,1,TRUE,"w3codebox") -> v1
c(3,1,TRUE,"w3codebox") ->> v2 
print(v1)
print(v2)

The output of the above code is:

[1] "3"      "1"      "TRUE"   "w3codebox"
[1] "3"      "1"      "TRUE"   "w3codebox"
[1] "3"      "1"      "TRUE"   "w3codebox"
[1] "3"      "1"      "TRUE"   "w3codebox"
[1] "3"      "1"      "TRUE"   "w3codebox"

Other operators

R language also includes some special operators.

OperatorDescription
:Colon operator, used to create a vector of a series of numbers.
%in%Used to determine if an element is in the vector, returns a boolean value, returns TRUE if it exists, and FALSE if it does not.
%*%Used for matrix multiplication with its transpose.
# 1 to 10 The vector
v <- 1:10
print(v) 
# Determine if a number is in the vector v
v1 <- 3
v2 <- 15
print(v1 %in% v) 
print(v2 %in% v) 
# Matrix multiplication with its transpose
M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE)
t = M %*% t(M)
print(t)

The output of the above code is:

[1]  1  2  3  4  5  6  7  8  9 10
[1] TRUE
[1] FALSE
     [,1] [,2]
[1,]   65   82
[2,]   82  117

Mathematical functions

Common for some mathematical functions are:

FunctionDescription
sqrt(n)The square root of n
exp(n)The nth power of the natural constant e,
log(m,n)The logarithmic function of m, returns the power of n that equals m
log10(m)Is equivalent to log(m,10)

The following examples demonstrate the application of mathematical functions:

> sqrt(4)
[1] 2
>  exp(1)
[1] 2.718282
>  exp(2)
[1] 7.389056
>  log(2,4)
[1] 0.5
>  log10(10000)
[1] 4

Rounding Functions:

NameParameter ModelMeaning
round(n)Round n to the nearest integer
 (n, m)Round n to m decimal places
ceiling(n)Ceiling n
floor(n)Floor n

)

>  round(1.5)
[1] 2
>  round(2.5)
[1] 2
>  round(3.5)
[1] 4
>  round(4.5)
[1] 4

AttentionNote that the round function in R may 'discard the five' in some cases.

When the integer part is even, the five will also be discarded, which is different from C language.

R's trigonometric functions are in radians:

>  sin(pi/6)
[1] 0.5
>  cos(pi/4)
[1] 0.7071068
>  tan(pi/3)
[1] 1.732051

Inverse Trigonometric Functions:

>  asin(0.5)
[1] 0.5235988
>  acos(0.7071068)
[1] 0.7853981
>  atan(1.732051)
[1] 1.047198

If you have studied probability theory and statistics, you should be familiar with the following probability distribution functions because R language is designed for mathematicians and is often used:

>  dnorm(0)
[1] 0.3989423
>  pnorm(0)
[1] 0.5
>  qnorm(0.95)
[1] 1.644854
>  rnorm(3, 5, 2)  # Generate 3 average of 5, standard deviation of 2 normal random numbers
[1] 4.177589 6.413927 4.206032

These four are all used to calculate normal distributions. Their names end with 'norm', representing 'normal distribution'.

There are four prefixes for the names of distribution functions:

  • d - Probability Density Function

  • p - Probability Density Integral Function (integral from infinitesimal to x)

  • q - Quantile Functions

  • r - Random Number Functions (often used in probability simulation)

NoteSince this tutorial does not elaborate on the mathematical theory of probability distribution, a detailed explanation of the mathematical theory related to probability distribution is not provided. R language, in addition to containing normal distribution functions, also has common distribution functions such as Poisson distribution (pois, Poisson). If you want to learn more, you can study "Probability Theory and Mathematical Statistics".