文章

ASML R Language

ASML R Language

R 语言入门

一、datatype数据类型

1.基本类型

  1. 数值型 numeric

    • 整型 integer
    • 浮点型 double
1
2
3
4
5
6
7
8
a <- 1 # 其实得到的是1.0,因为默认是double
b <- 0.1

is.integer(a) # False
is.double(b) # True,因为默认是double

a <- as.integer(1)
is.integer(a) # True
  1. 字符串 character
1
2
3
4
5
6
7
8
9
c <- "hello world"
# 操作character的函数:

# 粘贴
paste("hello world", "123", sep = "_") #有间隔符
paste0("hello world", "123") #无间隔符

#子串, R语言的索引从1开始
substr1 <- substr("hello world", start = 2, stop = 5)
  1. 逻辑型 logic
1
2
3
4
a_logical_T <- 'TRUE' #T也可以
a_logical_F <- FALSE #F也可以
a_logical_T <- as.logical(a_logical_T) #TRUE
is.logical(a_logical_T) #TRUE

注意:必须要重新赋值才行,不然函数返回的结果没有赋值给任何变量

2.多维数据类型

  1. Vector 向量一维

只有三种向量:numeric vector, character vector, logical vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# numeric
b_vector_1 <- c(1, 2, 3) # [1,2,3]
b_vector_2 <- c(1:3) # [1,2,3]
b_vector_3 <- c(1, 8 : 10) # [1,8,9,10]

#character
b_vector_4 <- c("a", "b" , "c")

#logical
b_vector_5 <- c(TRUE, FALSE)

#长度
length(b_vector_1)

c_vector <- c(b_vector_1, b_vector_3) #相当于合并向量
c_vector[c(1,2,3)] #取第1,2,3个值
print(c_vector) # 1,2,3,1,8,9,10

# vector替换,取多少给多少
# 赋值"f","g"给向量的第一个和第二个项
b_vector_4[c(1,2)] <- c("f", "g")
b_vector_4

# 先给a赋值2,再给b_vector_3[2]赋值20
b_vector_3[a <- 2] <- 20
b_vector_3
  1. Matrix 二维
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#matrix,第一个参数是一个向量,必须可以整除nrow;nrol可不写,1到50默认按照列排序,如果byrow就按照行排序
matrix1 <- matrix(c(1:50), nrow = 5)
matrix2 <- matrix(c(1:50), nrow = 5, ncol = 10, byrow = TRUE)

#取值
matrix1[5, 5]
matrix1[5, 5:7]
matrix1[1:5, 5:7]

# 赋值,取多少给多少
matrix1[5, 5] <- 3
matrix1[5, 5:7] <- c(1,2,3)
matrix1[1:5, 5:7] <- matrix(c(1:15), nrow = 5)

length(matrix1[1:5, 5:7])
nrow(matrix1) #5
ncol(matrix1) #10
  • Matrix运算
1
2
3
4
5
6
7
8
9
10
# 乘法
Matrix3*Matrix3
Matrix3*c(1:10) #第1-10行分别乘1-10
# 加法
Matrix3+Matrix3
# sum
sum(Matrix3)
# apply,第二个参数1是行,2是列,后面的sum可以替换成其他函数
apply(Matrix3,1,sum)

  1. DataFrame

接受的参数:多个等长的向量,参数的名字转换为列名

1
2
3
4
5
6
7
8
# data frame
name <- c("Bob", "Anna", "Li", "Wang")
score <- c(90, 99, 100, 95)
class <- c(1, 2, 1, 2)
student_score <- data.frame(name = name,
                            score = score,
                            class = class)
student_score

取值赋值和matrix基本一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 给行命名,取的值为student_score的第一列 [, 1]
rownames(student_score) <- student_score[, 1]
# 取行名为Bob,Anna的行 [c("Bob", "Anna"), ]
student_score_2line <- student_score[c("Bob", "Anna"), ]
student_score_2line

# 取行名为Bob,Anna的,列为score,class的dataframe
student_score_data <- student_score[c("Bob", "Anna"), 
                                    c("score", "class")]
student_score_data

# 使用$符号取列,取到的是一个向量
student_score_col <- student_score$score
student_score_col
# 取向量的部分
student_score_col_part <- student_score$class[c(2,3)]
student_score_col_part

in和which用于条件判断:

1
2
3
4
5
6
7
# in 下面的代码表示获得逻辑值,用于匹配那些行的name在 Bob和Anna里面
student_score_in <- student_score$name %in% c("Bob", "Anna")
student_score_in

# which 一般用于条件判断,直接返回匹配条件判断的行号
student_score_which <- which(student_score$score > 95)
student_score_which

行列名的替换:

1
2
3
4
# 行列名的替换,只要数量相同就行
rownames(student_score) <- student_score[, 1]
colnames(student_score) <- student_score[1, ]
student_score
  1. List

是一种特殊的向量,每个元素可以是不同的数据类型

1
2
3
4
5
6
list1 <- list(c(1,2),"hello",FALSE,matrix(c(1:100),nrow=100))
list1
# 单层中括号取出的还是一个list
list1[4]
# 双层中括号取出的才是list内部的数据
list1[[4]]

t.test() t检验的输出值是一个list

二、loop structure循环结构

1.For 循环

for循环类似python, 除了要加括号

1
2
3
4
5
6
7
8
9
10
11
12
# 累加
k <- 0
for (i in c(1:10)){
    k <- k + i
    print(k)
}

# 打印名字
names <- c("Bob", "Anna", "John")
for (name in names){
    print(name)
}

2.While 循环

1
2
3
4
5
6
# while循环
i <- 0
while(i < 10) {
    print(i)
    i <- i + 1
}

3. If else

ifelse合体用法相当于一个函数

1
2
3
4
5
6
7
8
9
10
11
# if else
a <- 0
b <- 1
if(a > b){
    print(">")
}else{
    print("<")
}

ifelse(a>b,">","<")

4.switch

一般用于ifelse分支太多

1
2
3
4
5
6
7
8
9
action <- "add"

result <- switch(action,
                 add = "You selected addition.",
                 subtract = "You selected subtraction.",
                 multiply = "You selected multiplication.",
                 "Invalid action.") # default

print(result)

三、function 函数

1.函数定义

1
2
3
4
my_func <- function(){
  ···
  return (xxx)
}

四、others 其他补充

1.打印print

1
print(xxx)

2.切割字符串strsplit()

类似js的split

1
strsplit("ab,cd",split="c") # ab cd

3.匹配字符串grep()

1
grep("ab",c("ab","abc","ab")) # 1 2

4.替换字符串sub()

1
sub("ab","xx",c("abc","abd")) #xxc xxd

5.向量长度length()

6.向量求和sum()

类似python的sum

1
sum(c(1:100))

7.求均值mean()

同python的np.mean()

8.排序sort

默认升序,NA不在最后

1
sort(x, decreasing = FALSE, na.last = TRUE)

9.判断数据类型is

1
2
str <- "abc"
is.character(str)

10.数据类型转换as

1
2
bool <- TRUE
str_bool <- as.character(bool)

11.中位数 median

1
median(arr)

12.取字串substr

1
substr(str,start,end)

13.拼接字符串paste,paste0

1
2
3
4
5
6
7
8
9
c <- "hello world"
# 操作character的函数:

# 粘贴
paste("hello world", "123", sep = "_") #有间隔符
paste0("hello world", "123") #无间隔符

#子串, R语言的索引从1开始
substr1 <- substr("hello world", start = 2, stop = 5)
本文由作者按照 CC BY 4.0 进行授权