Basic syntax, basic operators and functions of R language

Basic syntax, basic operations and functions of R language

The following code details the basic syntax and arithmetic operators, relational operators, logical operators and some basic mathematical functions of R language

Mathematical operator

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

prioritySymbolmeaning
1()brackets
2^Power operation
3%%Division and remainder
%/%to be divisible by
4*multiplication
/division
5+addition
-subtraction

Relational operator

The following table lists the relational operators supported by R language. The relational operator compares two vectors, compares each element of the first vector with the second vector, and returns a Boolean value.

operatordescribe
>Judge whether each element of the first vector is larger than the corresponding element of the second vector.
<Judge whether each element of the first vector is smaller 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.
!=Judge whether each element of the first vector is not equal to the corresponding element of the second vector.
>=Judge whether each element of the first vector is greater than or equal to the corresponding element of the second vector.
<=Judge whether each element of the first vector is less than or equal to the corresponding element of the second vector.

Logical operator

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

All numbers greater than 1 are TRUE.

The logical operator compares two vectors, compares each element of the first vector with the second vector, and the result returns a Boolean value.

operatordescribe
&The element logic 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 it is FALSE.
Element logic or operator, which 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, FALSE is returned.
!Logical non operator, which 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.
&&The logical and operator only judges the first element of two vectors. If both elements are TRUE, the result is TRUE, otherwise it is FALSE.
||The logical or operator only judges the first element of two vectors. If one of the two elements is TRUE, the result is TRUE. If both are FALSE, the result is FALSE.

Assignment Operators

R language variables can be assigned using left, right, or equal operators.

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

operatordescribe
<− = <<−Assign to the left.
−> −> >Assign to the right.

example

#Assignment < -- >=
x <- 1
x1 = 1
1 -> x2
x3 <- x1+x2


#Output print() function
hello <- "hello world"
print(hello)
print(123)


#working directory
  # getwd() gets the current working directory
  # setwd() sets the current working directory
print(getwd())
setwd("D:/Documents")
print(getwd())


#Mathematical operation of R language
a <- 1
b <- 5
print(a+b)#addition
print(b-a)#subtraction
c <- 8
d <- 2
e <- 5
print(c^d)#Power
print(c*d)#multiplication
print(c/e)#division
print(c%/%e)#to be divisible by
print(c%%e)#Division and remainder


#Relational operation of R language
g <- c(2,4,6,9)
h <- c(1,4,7,9)
print(g>h)#Judge whether each element of the first vector is larger than the corresponding element of the second vector.
print(g<h)#Judge whether each element of the first vector is smaller than the corresponding element of the second vector.
print(g == h)#Determine whether each element of the first vector is equal to the corresponding element of the second vector.
print(g!=h)#Judge whether each element of the first vector is not equal to the corresponding element of the second vector.
print(g>=h)#Judge whether each element of the first vector is greater than or equal to the corresponding element of the second vector.
print(g<=h)#Judge whether each element of the first vector is less than or equal to the corresponding element of the second vector.


#Logical operation of R language
i <- c(3,1,TRUE,2+3i)
j <- c(4,1,FALSE,2+3i)

print(i&j)
print(i|j)
print(!i)
# &&, | only the first element is compared
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)


#Mathematical functions of R language
print(sqrt(4))#Square root of sqrt(n) n
print(exp(1))#exp(n) the nth power of the natural constant e
print(log(2,4))#Log (m, n) is a logarithmic function of M, which returns the power of N equal to M
print(log10(1000))#Equivalent to log(m,10)
#Rounding function
print(round(3.5))#round(n) round n
print(round(4.5))#In some cases, the round function in R may "round off five". When the rounding bit is even, five will also be rounded off
print(round(4.55514,3))#round(n,m) keep m decimal places for N and round
print(ceiling(5.1))#ceiling(n) round n up
print(floor(6.6))#floor(n) rounds n down
#trigonometric function
print(sin(pi/6))#sin(n) sine function
print(cos(pi/3))#cos(n) cosine function
print(tan(pi/4))#tan(n) tangent function


The operation results are as follows

> #Assignment < -- >=
> x <- 1
> x1 = 1
> 1 -> x2
> x3 <- x1+x2
> 
> 
> #Output print() function
> hello <- "hello world"
> print(hello)
[1] "hello world"
> print(123)
[1] 123
> 
> 
> #working directory
>   # getwd() gets the current working directory
>   # setwd() sets the current working directory
> print(getwd())
[1] "D:/Documents"
> setwd("D:/New folder")
> print(getwd())
[1] "D:/New folder"
> 
> 
> #Mathematical operation of R language
> a <- 1
> b <- 5
> print(a+b)#addition
[1] 6
> print(b-a)#subtraction
[1] 4
> c <- 8
> d <- 2
> e <- 5
> print(c^d)#Power
[1] 64
> print(c*d)#multiplication
[1] 16
> print(c/e)#division
[1] 1.6
> print(c%/%e)#to be divisible by
[1] 1
> print(c%%e)#Division and remainder
[1] 3
> 
> 
> #Relational operation of R language
> g <- c(2,4,6,9)
> h <- c(1,4,7,9)
> print(g>h)#Judge whether each element of the first vector is larger than the corresponding element of the second vector.
[1]  TRUE FALSE FALSE FALSE
> print(g<h)#Judge whether each element of the first vector is smaller than the corresponding element of the second vector.
[1] FALSE FALSE  TRUE FALSE
> print(g == h)#Determine whether each element of the first vector is equal to the corresponding element of the second vector.
[1] FALSE  TRUE FALSE  TRUE
> print(g!=h)#Judge whether each element of the first vector is not equal to the corresponding element of the second vector.
[1]  TRUE FALSE  TRUE FALSE
> print(g>=h)#Judge whether each element of the first vector is greater than or equal to the corresponding element of the second vector.
[1]  TRUE  TRUE FALSE  TRUE
> print(g<=h)#Judge whether each element of the first vector is less than or equal to the corresponding element of the second vector.
[1] FALSE  TRUE  TRUE  TRUE
> 
> 
> #Logical operation of R language
> i <- c(3,1,TRUE,2+3i)
> j <- c(4,1,FALSE,2+3i)
> 
> print(i&j)
[1]  TRUE  TRUE FALSE  TRUE
> print(i|j)
[1] TRUE TRUE TRUE TRUE
> print(!i)
[1] FALSE FALSE FALSE FALSE
> # &&, | only the first element is compared
> v <- c(3,0,TRUE,2+2i)
> t <- c(1,3,TRUE,2+3i)
> print(v&&t)
[1] TRUE
> 
> v <- c(0,0,TRUE,2+2i)
> t <- c(0,3,TRUE,2+3i)
> print(v||t)
[1] FALSE
> 
> 
> #Mathematical functions of R language
> print(sqrt(4))#Square root of sqrt(n) n
[1] 2
> print(exp(1))#exp(n) the nth power of the natural constant e
[1] 2.718282
> print(log(2,4))#Log (m, n) is a logarithmic function of M, which returns the power of N equal to M
[1] 0.5
> print(log10(1000))#Equivalent to log(m,10)
[1] 3
> #Rounding function
> print(round(3.5))#round(n) round n
[1] 4
> print(round(4.5))#In some cases, the round function in R may "round off five". When the rounding bit is even, five will also be rounded off
[1] 4
> print(round(4.55514,3))#round(n,m) keep m decimal places for N and round
[1] 4.555
> print(ceiling(5.1))#ceiling(n) round n up
[1] 6
> print(floor(6.6))#floor(n) rounds n down
[1] 6
> #trigonometric function
> print(sin(pi/6))#sin(n) sine function
[1] 0.5
> print(cos(pi/3))#cos(n) cosine function
[1] 0.5
> print(tan(pi/4))#tan(n) tangent function

Keywords: Java R Language Data Analysis

Added by john6384 on Fri, 19 Nov 2021 06:21:46 +0200