R Basic Operations

This chapter introduces simple operations in R language.

Assignment

In general languages, assignment is=the = sign, but R language is a mathematical language, so the assignment symbol is very similar to the pseudocode in our mathematics textbooks; it is a left arrow<- :

Example

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

The execution result of the above code is:

[1] 579

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

In terms of operation,<the symbol and-symbols are not easy to type characters, which makes many programmers uncomfortable. Therefore, relatively new versions of R language also support=as an assignment operator:

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

This is also a valid R program.

Note:It is hard to verify from which version of R started supporting=assignment, but the R version used in this tutorial is 4.0.0.

Mathematical Operators

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

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

The following example demonstrates simple mathematical operations:

Example

> 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
[1] Inf
> 3 %% 2^2
[1] 3
> 10 / 3 %/% 2
[1] 10

Relational Operators

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

OperatorDescription
>Determine whether each element of the first vector is greater than the corresponding element of the second vector.
<Determine whether each element of the first vector is less than the corresponding element of the second vector.
==Determine whether each element of the first vector is equal to the corresponding element of the second vector.
!=Determine whether each element of the first vector is not equal to the corresponding element of the second vector.
>=Determine whether each element of the first vector is greater than or equal to the corresponding element of the second vector.
<=Determine whether each element of the first vector is less than or equal to the corresponding element of the second vector.

Example

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 after executing the above code is:

[1]  TRUE FALSE FALSE FALSE
[1] FALSE FALSE  TRUE FALSE
[1] FALSE  TRUE FALSE  TRUE
[1]  TRUE FALSE  TRUE FALSE
[1]  TRUE  TRUE FALSE  TRUE
[1] FALSE  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.

Non-zero numbers (positive or negative) are all TRUE.

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

OperatorDescription
&Element-wise logical AND operator. It 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, it is FALSE.
|Element-wise logical OR operator. It combines each element of the first vector with the corresponding element of the second vector. If either of the two elements is TRUE, the result is TRUE; if both are FALSE, it returns FALSE.
!Logical NOT operator. It returns the opposite logical value of each element of the vector. If the element is TRUE, it returns FALSE; if the element is FALSE, it returns TRUE.
&&Logical AND operator. It only evaluates the first element of the two vectors. If both elements are TRUE, the result is TRUE; otherwise, it is FALSE.
||Logical OR operator. It only evaluates the first element of the two vectors. If either of the two elements is TRUE, the result is TRUE; if both are FALSE, it returns FALSE.

Example

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
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 after executing 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 be assigned using leftward, rightward, or equal operators.

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

OperatorDescription

<−

=

<<−

Assign to the left.

−>

−>>

Assign to the right.

Example

# Assign to the left
v1 <- c(3,1,TRUE,"example")
v2 <<- c(3,1,TRUE,"example")
v3 = c(3,1,TRUE,"example")
print(v1)
print(v2)
print(v3)


# Assign to the right
c(3,1,TRUE,"example") -> v1
c(3,1,TRUE,"example") ->> v2
print(v1)
print(v2)

The output after executing the above code is:

[1] "3"      "1"      "TRUE"   "example"
[1] "3"      "1"      "TRUE"   "example"
[1] "3"      "1"      "TRUE"   "example"
[1] "3"      "1"      "TRUE"   "example"
[1] "3"      "1"      "TRUE"   "example"

Other Operators

R language also contains some special operators.

OperatorDescription
:Colon operator, used to create a vector of a series of numbers.
%in%Used to determine whether an element is in a vector. It returns a boolean value: TRUE if present, FALSE if not.
%*%Used to multiply a matrix by its transpose.

Example

# A vector from 1 to 10
v <- 1:10
print(v)

# Determine whether a number is in vector v
v1 <- 3
v2 <- 15
print(v1 %in% v)
print(v2 %in% v)

# Multiply a matrix by 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 after executing 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

Some common mathematical functions are:

FunctionDescription
sqrt(n)Square root of n
exp(n)The nth power of the natural constant e,
log(m,n)Logarithm function of m, returns the power of n that equals m
log10(m)Equivalent to log(m, 10)

The following example demonstrates the application of mathematical functions:

Example

> 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)Round n up
floor(n)Round n down

The following example demonstrates the application of rounding functions:

Example

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

Note: The round function in R may sometimes "discard five".

When the rounding digit is even, five is also discarded, which is different from C language.

R's trigonometric functions are in radians:

Example

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

Inverse trigonometric functions:

Example

> 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, they are often used:

Example

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

These four are all functions used to calculate the normal distribution. Their names all end with "norm", which represents "normal distribution".

There are four prefixes for distribution function names:

  • d- Probability density function
  • p- Probability density integral function (integral from negative infinity to x)
  • q- Quantile function
  • r- Random number function (often used in probability simulation)

Note: Since this tutorial is not a tutorial on mathematical professional theory, it will not explain in detail the mathematical theory of probability distributions. In addition to the normal distribution function, R language 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".

Other Extensions