R Basic Operations
This chapter introduces simple operations in R language.
Assignment
In most languages, assignment is done using the = symbol, but since R is a mathematical language, its assignment symbol is similar to pseudocode in math books, which is a left arrow <-
:
Example
a <- 123
b <- 456
print(a + b)
The result of the above code execution:
[1] 579
This assignment symbol is a formal advantage and operational disadvantage in R: it is more suitable for mathematicians, as not all mathematicians are accustomed to using = as an assignment symbol.
Operationally, the <
and -
symbols are not easy to type, which can make many programmers uncomfortable. Therefore, newer versions of R also support =
as an assignment operator:
a = 123
b = 456
print(a + b)
This is also a valid R program.
Note: It is difficult to trace from which version of R the = assignment was supported, 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 operation:
Priority | Symbol | Meaning |
---|---|---|
1 | () | Parentheses |
2 | ^ | Exponentiation |
3 | %% | Modulus (remainder of division) |
%/% | 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 table below lists the relational operators supported by R. 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.
Operator | Description |
---|---|
> | Checks if each element of the first vector is greater than the corresponding element of the second vector. |
< | Checks if each element of the first vector is less than the corresponding element of the second vector. |
== | Checks if each element of the first vector is equal to the corresponding element of the second vector. |
!= | Checks if each element of the first vector is not equal to the corresponding element of the second vector. |
>= | Checks if each element of the first vector is greater than or equal to the corresponding element of the second vector. |
<= | Checks if 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 of the above code execution 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 table below lists the logical operators supported by R, which can be used with numeric, logical, and complex vectors.
Non-zero numbers (positive or negative) are considered 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.
Operator | Description | ||
---|---|---|---|
& | Element-wise 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-wise logical OR operator, combines each element of the first vector with the corresponding element of the second vector. If at least one element is TRUE, the result is TRUE, if both are FALSE, the result is FALSE. | ||
! | Logical NOT operator, returns the opposite logical value for each element of the vector. If the element is TRUE, it returns FALSE, if the element is FALSE, it returns TRUE. | ||
&& | Logical AND operator, only compares the first elements of two vectors. If both elements are TRUE, the result is TRUE, otherwise FALSE. | ||
Logical OR operator, only compares the first elements of two vectors. If at least one element is TRUE, the result is TRUE, if both are FALSE, the result is 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 elements
[1] TRUE TRUE FALSE TRUE
[1] TRUE TRUE TRUE TRUE
[1] FALSE FALSE FALSE FALSE
[1] TRUE
[1] FALSE
Assignment Operators
In R, variables can be assigned using leftward, rightward, or equal operators.
The following table lists the assignment operators supported by R.
Operator | Description |
---|---|
<− = <<− | Leftward assignment. |
−> −>> | Rightward assignment. |
Example
# Leftward assignment
v1 <- c(3,1,TRUE,"tutorialpro")
v2 <<- c(3,1,TRUE,"tutorialpro")
v3 = c(3,1,TRUE,"tutorialpro")
print(v1)
print(v2)
print(v3)
# Rightward assignment
c(3,1,TRUE,"tutorialpro") -> v1
c(3,1,TRUE,"tutorialpro") ->> v2
print(v1)
print(v2)
Executing the above code outputs:
[1] "3" "1" "TRUE" "tutorialpro"
[1] "3" "1" "TRUE" "tutorialpro"
[1] "3" "1" "TRUE" "tutorialpro"
[1] "3" "1" "TRUE" "tutorialpro"
[1] "3" "1" "TRUE" "tutorialpro"
Miscellaneous Operators
R also includes some special operators.
Operator | Description |
---|---|
: | Colon operator, used to create a vector of sequential numbers. |
%in% | Used to check if an element is in a vector, returns a boolean value, TRUE if present, FALSE otherwise. |
%*% | Used for matrix multiplication with its transpose. |
Example
# Vector from 1 to 10
v <- 1:10
print(v)
# Check if numbers are in 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)
Executing the above code outputs:
[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 mathematical functions include:
Function | Description |
---|---|
sqrt(n) | Square root of n |
exp(n) | Exponential function, e raised to the power of n |
log(m,n) | Logarithm of m with base n |
log10(m) | Logarithm of m with base 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:
Name | Parameter Model | Meaning |
---|---|---|
round | (n) | Rounds n to the nearest integer |
(n, m) | Rounds n to m decimal places | |
ceiling | (n) | Rounds n up to the nearest integer |
floor | (n) | Rounds n down to the nearest integer |
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 "drop the five" in some cases.
When the rounding digit is even, the five is also dropped, which is different from C language.
R's trigonometric functions use 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, as R is designed for mathematical professionals and these functions are frequently used:
Example
> dnorm(0)
[1] 0.3989423
> pnorm(0)
[1] 0.5
> qnorm(0.95)
[1] 1.644854
> rnorm(3, 5, 2) # Generates 3 normal random numbers with a mean of 5 and a standard deviation of 2
[1] 4.177589 6.413927 4.206032
These four functions are used to compute the normal distribution. Their names all end with "norm," representing "normal distribution."
The prefixes of the distribution function names are as follows:
- d - Probability Density Function
- p - Cumulative Distribution Function (integral from minus infinity to x)
- q - Quantile Function
- r - Random Number Generator (commonly used for probability simulations)
Note: This tutorial does not delve into the mathematical theory of probability distributions, as it is not intended to be a comprehensive guide on mathematical theory. In addition to normal distribution functions, R also includes common distribution functions such as Poisson distribution (pois, Poisson). For more detailed information, one can study "Probability Theory and Mathematical Statistics."