R语言学习笔记-数据类型:向量,矩阵,数组,数据框,列表

一、向量(vector)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
> A <- c(1,2,3,4,5,6)
> B <- c("A","B","C","D","E")
> A
[1] 1 2 3 4 5 6
> B
[1] "A" "B" "C" "D" "E"
> class(A)
[1] "numeric"
> mode(A)
[1] "numeric"
> typeof(A)
[1] "double"
> class(B)
[1] "character"
> mode(B)
[1] "character"
> typeof(B)
[1] "character"

二、矩阵(matrix)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> var1=sample(31:60,25,replace=T)
# 矩阵的元素类型是一致的,或数值,或字符串;维度为二,即二维数组;
> C <- matrix(var1,nrow=5)
> C
[,1] [,2] [,3] [,4] [,5]
[1,] 43 32 54 41 56
[2,] 56 45 37 57 38
[3,] 50 50 40 32 50
[4,] 34 56 49 36 42
[5,] 56 41 33 40 46

> class(C)
[1] "matrix"
> mode(C)
[1] "numeric"
> typeof(C)
[1] "integer"

三、数组(array)

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 创建向量var2;
> var2 <- sample(1:100,81)
> var2
[1] 87 66 38 84 95 19 29 32 72 98 70 37 53 54 16 15 64 58 2
[20] 91 11 81 57 35 18 69 61 62 74 40 76 96 73 85 22 44 31 6
[39] 51 36 25 82 77 97 10 27 39 24 79 55 78 7 30 92 49 63 20
[58] 99 56 65 46 60 28 13 4 83 1 94 59 52 86 48 100 88 34 23
[77] 71 67 47 3 89
> class(var2)
[1] "integer"
> mode(var2)
[1] "numeric"
> typeof(var2)
[1] "integer"
# 创建数组C;数组的维数不限,但数据类型必须一致,即或都是数值,或都是字符串;
> C <- array(var2,dim=c(3,3,9))
> C
, , 1

[,1] [,2] [,3]
[1,] 87 84 29
[2,] 66 95 32
[3,] 38 19 72

, , 2

[,1] [,2] [,3]
[1,] 98 53 15
[2,] 70 54 64
[3,] 37 16 58

, , 3

[,1] [,2] [,3]
[1,] 2 81 18
[2,] 91 57 69
[3,] 11 35 61

, , 4

[,1] [,2] [,3]
[1,] 62 76 85
[2,] 74 96 22
[3,] 40 73 44

, , 5

[,1] [,2] [,3]
[1,] 31 36 77
[2,] 6 25 97
[3,] 51 82 10

, , 6

[,1] [,2] [,3]
[1,] 27 79 7
[2,] 39 55 30
[3,] 24 78 92

, , 7

[,1] [,2] [,3]
[1,] 49 99 46
[2,] 63 56 60
[3,] 20 65 28

, , 8

[,1] [,2] [,3]
[1,] 13 1 52
[2,] 4 94 86
[3,] 83 59 48

, , 9

[,1] [,2] [,3]
[1,] 100 23 47
[2,] 88 71 3
[3,] 34 67 89
# 查看类型;
> class(C)
[1] "array"
> mode(C)
[1] "numeric"
> typeof(C)
[1] "integer"

四、数据框(data.frame)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
> var3 <- LETTERS[1:8]
> var4 <- sample(3:12,8,replace=T)
# 数据框的元素类型可不一致,但组成数据框的向量的维数必须相等;
> D <- data.frame(var3,var4)
> D
var3 var4
1 A 11
2 B 10
3 C 9
4 D 6
5 E 10
6 F 11
7 G 7
8 H 10
# 查看类型;
> class(D)
[1] "data.frame"
> mode(D)
[1] "list"
> typeof(D)
[1] "list"

五、列表(list)

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 列表是一个集合,数据类型可不一致,长度也可不一致,可以由向量,矩阵,数组,数据框,函数,甚至是列表组成;
> E <- list(A,B,C,D)
> E
[[1]]
[1] 1 2 3 4 5 6

[[2]]
[1] "A" "B" "C" "D" "E"

[[3]]
, , 1

[,1] [,2] [,3]
[1,] 87 84 29
[2,] 66 95 32
[3,] 38 19 72

, , 2

[,1] [,2] [,3]
[1,] 98 53 15
[2,] 70 54 64
[3,] 37 16 58

, , 3

[,1] [,2] [,3]
[1,] 2 81 18
[2,] 91 57 69
[3,] 11 35 61

, , 4

[,1] [,2] [,3]
[1,] 62 76 85
[2,] 74 96 22
[3,] 40 73 44

, , 5

[,1] [,2] [,3]
[1,] 31 36 77
[2,] 6 25 97
[3,] 51 82 10

, , 6

[,1] [,2] [,3]
[1,] 27 79 7
[2,] 39 55 30
[3,] 24 78 92

, , 7

[,1] [,2] [,3]
[1,] 49 99 46
[2,] 63 56 60
[3,] 20 65 28

, , 8

[,1] [,2] [,3]
[1,] 13 1 52
[2,] 4 94 86
[3,] 83 59 48

, , 9

[,1] [,2] [,3]
[1,] 100 23 47
[2,] 88 71 3
[3,] 34 67 89


[[4]]
var3 var4
1 A 11
2 B 10
3 C 9
4 D 6
5 E 10
6 F 11
7 G 7
8 H 10
# 查看类型;
> class(E)
[1] "list"
> mode(E)
[1] "list"
> typeof(E)
[1] "list"
------ 本文结束 ------
坚持原创文章分享,您的支持将鼓励我继续创作!