本篇文章是对R语言入门的知识点整理,知识点主要来源于https://www.datacamp.com/,推荐在该网站上进行学习R、python等数据科学相关知识。
(注:本文主要是自学过程中的知识点整理,方便个人后期查看,不喜勿喷并请尊重原创)
本文主要分为以下3个部分:
1.Introduction
2.Vectors
3.Matrices
一、 Introduction
1. 基础运算
+、-、*、/、^、%%
example
# An addition
5 + 5
# A subtraction
5 - 5
# A multiplication
3 * 5
# A division
(5 + 5) / 2
# Exponentiation
2 ^ 5
# Modulo
28 %% 6
2. 变量赋值与输出
variable_nameexample and output
# Assign the value 42 to x x <- 42 # Print out the value of the variable x x # Change my_numeric to be 42 my_numeric <- 42 # Change my_character to be "universe" my_character <- "universe" # Change my_logical to be FALSE my_logical <- FALSE
3. 查看变量类型
class()
example and output
# Declare variables of different types my_numeric <- 42 my_character <- "universe" my_logical <- FALSE # Check class of my_numeric > class(my_numeric) [1] "numeric" > > # Check class of my_character > class(my_character) [1] "character" > > # Check class of my_logical > class(my_logical) [1] "logical"
二、 Vector
1. 创建Vector
c()
example
numeric_vector <- c(1, 2, 3)
character_vector <- c("a", "b", "c")
2. 命名Vector
names()
example
some_vector <- c("John Doe", "poker player")
names(some_vector) <- c("Name", "Profession")
output
Name Profession
"John Doe" "poker player"
3. Vector间的运算:对应元素进行运算
example and output
c(1, 2, 3) + c(4, 5, 6)
c(1 + 4, 2 + 5, 3 + 6)
c(5, 7, 9)
4. Vector的sum()函数与mean()函数
example
roulette_vector <- c(-24, -50, 100, -350, 10) sum(roulette_vector) mean(roulette_vector)
output
-314 -62.8
5. 数值比较
example
30 < 50
output
TRUE
6. 获取Vector中特定的元素(注意:与主流编程语言不同,R下标从1开始)
1)利用下标选取元素
访问一个元素
example
poker_vector <- c(140, -50, 20, -120, 240) poker_wednesday <- poker_vector[3]
output
20
访问多个元素——离散or连续
example
poker_vector <- c(140, -50, 20, -120, 240) poker_midweek <- poker_vector[c(2, 3, 4)]
output (打印出poker_midweek)
Tuesday Wednesday Thursday -50 20 -120访问多个元素——连续
example
roulette_vector <- c(-24, -50, 100, -350, 10) roulette_selection_vector <- roulette_vector[2:5] roulette_selection_vector
output
Tuesday Wednesday Thursday Friday -50 100 -350 10
example
poker_vector <- c(140, -50, 20, -120, 240) days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") names(poker_vector) <- days_vector poker_start <- poker_vector[c("Monday", "Tuesday", "Wednesday")] poker_start
output
Monday Tuesday Wednesday 140 -50 20
< for less than > for greater than <= for less than or equal to >= for greater than or equal to == for equal to each other != not equal to each other
example1 and output1
> c(4, 5, 6) > 5
[1] FALSE FALSE TRUE
example2 and output2
> # Poker and roulette winnings from Monday to Friday: > poker_vector <- c(140, -50, 20, -120, 240) > days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") > names(poker_vector) <- days_vector > > # Which days did you make money on poker? > selection_vector <- poker_vector > 0 > > # Select from poker_vector these days > poker_winning_days <- poker_vector[selection_vector] > > poker_winning_days Monday Wednesday Friday 140 20 240
三、Matrix
1. 创建Matrix
1) 简单创建matrix
matrix(?, byrow = ?, nrow = ?)
example and output
> matrix(1:9, byrow = TRUE, nrow = 3)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
>
参数含义解释:a. 1:9表示内容从1开始到9
b. byrow = TRUE表示按行进行填充,byrow = FALSE表示按列进行填充
c. nrow = 3表示构建3行
2) 利用Vector构建Matrix
matrix(vector_name)
example
# Box office Star Wars (in millions!)
new_hope <- c(460.998, 314.4)
empire_strikes <- c(290.475, 247.900)
return_jedi <- c(309.306, 165.8)
# Create box_office
box_office <- c(new_hope, empire_strikes, return_jedi)
# Construct star_wars_matrix
star_wars_matrix <- matrix(box_office, byrow = TRUE, nrow = 3)
output
> star_wars_matrix
[,1] [,2]
[1,] 460.998 314.4
[2,] 290.475 247.9
[3,] 309.306 165.8
2. 对Matrix进行命名
rownames(my_matrix) <- row_names_vector #对各行进行命名
colnames(my_matrix) <- col_names_vector #对各列进行命名
example
> # Vectors region and titles, used for naming
> region <- c("US", "non-US")
> titles <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi")
>
> # Name the columns with region
> colnames(star_wars_matrix) <- region
>
> # Name the rows with titles
> rownames(star_wars_matrix) <- titles
output
> # Print out star_wars_matrix
> star_wars_matrix
US non-US
A New Hope 460.998 314.4
The Empire Strikes Back 290.475 247.9
Return of the Jedi 309.306 165.8
3. 对Matrix行列进行求和统计
1)rowSums() : 按行进行求和统计
rowSums(my_matrix)
2)colSums() : 按列进行求和统计
colSums(my_matrix)
4. 对Matrix行列进行扩展
1)cbind() : 添加一列或多列
big_matrix <- cbind(matrix1, matrix2, vector1 ...)
2) rbind() : 添加一行或多行
big_matrix <- rbind(matrix1, matrix2, vector1 ...)
example and output
> # star_wars_matrix and star_wars_matrix2 are available in your workspace
> star_wars_matrix
US non-US
A New Hope 461.0 314.4
The Empire Strikes Back 290.5 247.9
Return of the Jedi 309.3 165.8
> star_wars_matrix2
US non-US
The Phantom Menace 474.5 552.5
Attack of the Clones 310.7 338.7
Revenge of the Sith 380.3 468.5
>
> # Combine both Star Wars trilogies in one matrix
> all_wars_matrix <- rbind(star_wars_matrix, star_wars_matrix2)
>
> all_wars_matrix
US non-US
A New Hope 461.0 314.4
The Empire Strikes Back 290.5 247.9
Return of the Jedi 309.3 165.8
The Phantom Menace 474.5 552.5
Attack of the Clones 310.7 338.7
Revenge of the Sith 380.3 468.5
>
5. 选择Matrix中的元素
1)选择Aij(第i行第j列的元素)
matrix[i, j]
2)选择某一行或者某一列
matrix[i, ] #选择第i行
matrix[ , j] #选择第j列
3)选择一个子矩阵
matrix[i1:i2, j1:j2] #选择第i1行到第i2行,第j1列到j2列
6. Matrix的运算
+、-、*、/ 是针对Matrix中的所有元素进行相应的操作
注意:matrix与matrix之间运用*时并非标准的矩阵乘法,而是对应项之间相乘
矩阵的标准乘法所用符号是%*%
后续R语言——入门系列2将整理factors、Dataframes and lists