knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE, out.width="100%") d <- seq(from = 1, to = 5, by = 0.5)
1 + 2; 1.5 + 2.4 3 - 4; -6 - 7 5 * 6; 23 * 14 7 / 8; -9 / 0.2
;
in R is used to connect two independent command lines.
Based on the above command, please solve the following question:
$$\frac{\frac{365}{12}}{\frac{28 + 24}{10} - (36 + 72) \times 5}$$
(365 / 12) / ((28 + 24)/10 - (36 + 72) * 5)
sqrt(9) 10^2 5%%3 #remainder log(11) log10(100) exp(12)
trigonometric functions:
sin(pi) cos(pi) tan(pi/2) acos(1) asin(1) atan(0.5)
tan(pi/2)
A number is written as a real number "a" multiplied by 10 to the power of "n". Here, E (exponential) is used to represent the power of 10. Here are some examples:
$$200 = 2e + 2, $$ $$0.002 = 2e-3,$$ $$333.3 = 3.333e + 2,$$ $$-45,000 = -4.5e + 4.$$
What does 2.8e + 10 represent?
format(2.8e+10, scientific = FALSE, big.mark = ',')
TRUE
to represent true, FALSE
to represent false, and you can also use T
and F
as abbreviations.!
), equal to (==
), not equal to (!=
), greater than (>
), less than (<
), and (&
), or (|
)>=
) and less than or equal to (<=
) can also be derived.Here are some examples:
1 == 2 1 != 2 1 > 2 1 < 2 1 >= 2 1 <= 2 1 & 2 > 2 1 | 2 > 2
What is the result of the following formula?
3 > 2 > 1
(3 > 2) & (2 > 1)
Many programming language beginners may feel uncomfortable when using double equal signs (
==
) to represent "equals". However, it can be viewed as a member of==, !=, >=, <=
.
In R, vectors are also the most basic way of representing data.
Variables can be viewed as "conceptually meaningful vectors".
a <- c(11, 12, 13, 14, 15)
c
: Concatenate/combinea <- c(11, 12, 13, 14, 15) a[2] a[5]
<-
: assign:
: a <- 1:5
TRUE
means keep something, FALSE
means don't keep somethingb <- 10 * 1:5 b
# How about the result? c(1.5:3)
How to represent $\vec {b}=(1, 3, 4, 5, 6, 7, 9)$?
b <- c(1, 3:7, 9)
seq()
seq
()
Repeat the above example:
a <- seq(from = 1, to = 5) a
The convenience of seq()
is that the step size can be defined:
c <- seq(from = 1, to = 5, by = 3) c d <- seq(from = 1, to = 5, by = 0.5) d
Another function of seq()
is to reflect the position of the element.
For example: how to choose the element of d
at the even-numbered position?
Hint: Determine the index → Find out whether each bit in the index is an even number (whether it can be divisible by 2), it is marked with
TRUE
→ On the basis of the second step, according to the rule of "TRUE
,FALSE
is not reserved", the elements with even numbers in the vector are reserved as a new vector.
d[seq(d) %% 2 == 0]
*
in R to output the dot product result.%o%
or use the outer()
command to calculate.a + b a - b a * b a / b
When the vectors used for calculation are of unequal length, the elements of the shorter vector will be cycled through in sequence, as shown in the following example:
a e <- c(1, 2) a * e
Cross-product in R can actually be used for more than just manipulating data.
For example, this functionality can be used to calculate the combination of the ten Heavenly Stems and twelve Earthly Branches in Chinese astrology using R.
tiangan <- c("甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸") dizhi <- c("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥")
Note: As you all know, there are ten Heavenly Stems and twelve Earthly Branches in Chinese astrology. There are a total of 120 combinations, but our ancestors selected 60 of them to form the basis of the Chinese zodiac. The selection method is to pair odd numbers with odd numbers and even numbers with even numbers.
tg_yang <- tiangan[seq(tiangan) %% 2 == 1] tg_yin <- tiangan[seq(tiangan) %% 2 == 0] dz_yang <- dizhi[seq(dizhi) %% 2 == 1] dz_yin <- dizhi[seq(dizhi) %% 2 == 0] outer(tg_yang, dz_yang, FUN = paste0) outer(tg_yin, dz_yin, FUN = paste0)
a #length length(a) #best value max(a) min(a) #average value mean(a) #median median(a) #variance var(a)
These commands can be used commonly in subsequent data-based variable operations.
$$ A_{2\times2} = \left(\begin{array}{cc} 1 & 3\ 2 & 4 \end{array}\right) $$
A <- matrix(1:4, nrow = 2) A
-R:
A[1, 2] A[1, ] A[ , 2]
B <- matrix(1:6, nrow = 3) B dim(B) t(B) t(t(B))
t(t(B))
is a nested approach.
In R, you can perform this operation by using
%*%
to concatenate two vectors that meet the defined properties.
B C <- matrix(7:12, nrow = 3) B + C B - C 10 * B D <- matrix(1:6, nrow = 2) D dim(B) dim(D) B %*% D
Use R to solve a system of three linear equations:
$$ \begin{equation} \begin{cases} x + y + z = 6, \ 3x + 2y + 4z = 9, \ 2x + 2y - 6z = 4. \end{cases} \end{equation} $$
$$ \begin{align} \vec{b} =& \vec{E}\vec{x},\ \begin{bmatrix} 6 \ 9 \ 4 \end{bmatrix} =& \begin{bmatrix} 1 & 1 & 1 \ 3 & 2 & 4 \ 2 & 2 & -6 \end{bmatrix} \begin{bmatrix} x \ y \ z \end{bmatrix} \end{align}. $$
E <- matrix(c(1, 3, 2, 1, 2, 2, 1, 4, -6), nrow = 3) E b <- c(6, 9, 4) x <- solve(E, b) x
One direct application of solving systems of equations is to solve regression equations.
Regression equations can typically be expressed in terms of linear algebra.
For example, a basic regression equation can be written in the following form, which is very similar to the system of equations we solved above:
$$ \begin{align} y& =& &\beta_0 + \beta_1x_1 + \beta_2x_2 + ... + \beta_nx_n + \epsilon\ \vec{y}& =& &\vec{x}\vec{\beta} + \vec{\epsilon}.\ \uparrow& & &\uparrow\uparrow\ \vec{b}& =& &\vec{E}\vec{x} \end{align} $$
!
、==
、!=
、>
、<
、&
、|
、>=
、<=
<-
%*%
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.