本文是生信技能树生信爆款入门课程R语言部分的讲到的一个重要知识点。
为加深理解,现在做下练习总结。
R语言数据类型主要包括
向量,数据框,矩阵,列表。
重点:数据框
1.数据框来源
(1)在R中新建
(2)由已有数据转换或处理得到
(3)从文件中读取
(4)内置数据集
2.新建和读取数据框
> options(stringsAsFactors = F)
> df <- data.frame(gene = c("gene1","gene2","gene3"),
+ sam = c("sample1","sample2","sample3"),
+ exp = c(32,34,45))
> df
gene sam exp
1 gene1 sample1 32
2 gene2 sample2 34
3 gene3 sample3 45
>
> df <- data.frame(gene = paste0("gene",1:3),
+ sam = paste0("sample",1:3),
+ exp = c(32,34,45))
> df
gene sam exp
1 gene1 sample1 32
2 gene2 sample2 34
3 gene3 sample3 45
>
> df2 <- read.csv("gene.csv")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'gene.csv': No such file or directory
> df2
Error: object 'df2' not found
>
> df <- data.frame(gene = paste0("gene",1:3),
+ sam1 = rnorm(3),
+ sam2 = rnorm(3),
+ sam3 = rnorm(3))
> df
gene sam1 sam2 sam3
1 gene1 0.07456498 -0.05612874 -0.4781501
2 gene2 -1.98935170 -0.15579551 0.4179416
3 gene3 0.61982575 -1.47075238 1.3586796
3.数据框属性描述
> dim(df)
[1] 3 4
> nrow(df)
[1] 3
> ncol(df)
[1] 4
> #
> rownames(df)
[1] "1" "2" "3"
> colnames(df)
[1] "gene" "sam1" "sam2" "sam3"
4.数据框取子集
> df[2,2]
[1] -1.989352
> df[2,]
gene sam1 sam2 sam3
2 gene2 -1.989352 -0.1557955 0.4179416
> df[,2]
[1] 0.07456498 -1.98935170 0.61982575
> df[c(1,3),1:2]
gene sam1
1 gene1 0.07456498
3 gene3 0.61982575
>
> df[,"gene"]
[1] "gene1" "gene2" "gene3"
> df[,c('gene','exp')]
Error in `[.data.frame`(df, , c("gene", "exp")) :
undefined columns selected
>
> df$exp #删掉exp,按tab键试试
NULL
> mean(df$exp)
[1] NA
Warning message:
In mean.default(df$exp) : 参数不是数值也不是逻辑值:回覆NA
5.数据框编辑
> #改一个格
> df[3,3]<- 5
> #改一整列
> df$exp<-c(12,23,50)
> #?
> df$abc <-c(23,15,37)
> df
gene sam1 sam2 sam3 exp abc
1 gene1 0.07456498 -0.05612874 -0.4781501 12 23
2 gene2 -1.98935170 -0.15579551 0.4179416 23 15
3 gene3 0.61982575 5.00000000 1.3586796 50 37
> #改行名和列名
> rownames(df) <- c("r1","r2","r3")
> #只修改某一行/列的名
> rownames(df)[2]="x"
6.数据框进阶
(1)行数较多的数据框可截取前/后几行查看
iris
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
7 4.6 3.4 1.4 0.3 setosa
8 5.0 3.4 1.5 0.2 setosa
9 4.4 2.9 1.4 0.2 setosa
10 4.9 3.1 1.5 0.1 setosa
11 5.4 3.7 1.5 0.2 setosa
12 4.8 3.4 1.6 0.2 setosa
13 4.8 3.0 1.4 0.1 setosa
14 4.3 3.0 1.1 0.1 setosa
15 5.8 4.0 1.2 0.2 setosa
16 5.7 4.4 1.5 0.4 setosa
17 5.4 3.9 1.3 0.4 setosa
18 5.1 3.5 1.4 0.3 setosa
19 5.7 3.8 1.7 0.3 setosa
20 5.1 3.8 1.5 0.3 setosa
21 5.4 3.4 1.7 0.2 setosa
22 5.1 3.7 1.5 0.4 setosa
23 4.6 3.6 1.0 0.2 setosa
24 5.1 3.3 1.7 0.5 setosa
25 4.8 3.4 1.9 0.2 setosa
26 5.0 3.0 1.6 0.2 setosa
27 5.0 3.4 1.6 0.4 setosa
28 5.2 3.5 1.5 0.2 setosa
29 5.2 3.4 1.4 0.2 setosa
30 4.7 3.2 1.6 0.2 setosa
31 4.8 3.1 1.6 0.2 setosa
32 5.4 3.4 1.5 0.4 setosa
33 5.2 4.1 1.5 0.1 setosa
34 5.5 4.2 1.4 0.2 setosa
35 4.9 3.1 1.5 0.2 setosa
36 5.0 3.2 1.2 0.2 setosa
37 5.5 3.5 1.3 0.2 setosa
38 4.9 3.6 1.4 0.1 setosa
39 4.4 3.0 1.3 0.2 setosa
40 5.1 3.4 1.5 0.2 setosa
41 5.0 3.5 1.3 0.3 setosa
42 4.5 2.3 1.3 0.3 setosa
43 4.4 3.2 1.3 0.2 setosa
44 5.0 3.5 1.6 0.6 setosa
45 5.1 3.8 1.9 0.4 setosa
46 4.8 3.0 1.4 0.3 setosa
47 5.1 3.8 1.6 0.2 setosa
48 4.6 3.2 1.4 0.2 setosa
49 5.3 3.7 1.5 0.2 setosa
50 5.0 3.3 1.4 0.2 setosa
51 7.0 3.2 4.7 1.4 versicolor
52 6.4 3.2 4.5 1.5 versicolor
53 6.9 3.1 4.9 1.5 versicolor
54 5.5 2.3 4.0 1.3 versicolor
55 6.5 2.8 4.6 1.5 versicolor
56 5.7 2.8 4.5 1.3 versicolor
57 6.3 3.3 4.7 1.6 versicolor
58 4.9 2.4 3.3 1.0 versicolor
59 6.6 2.9 4.6 1.3 versicolor
60 5.2 2.7 3.9 1.4 versicolor
61 5.0 2.0 3.5 1.0 versicolor
62 5.9 3.0 4.2 1.5 versicolor
63 6.0 2.2 4.0 1.0 versicolor
64 6.1 2.9 4.7 1.4 versicolor
65 5.6 2.9 3.6 1.3 versicolor
66 6.7 3.1 4.4 1.4 versicolor
67 5.6 3.0 4.5 1.5 versicolor
68 5.8 2.7 4.1 1.0 versicolor
69 6.2 2.2 4.5 1.5 versicolor
70 5.6 2.5 3.9 1.1 versicolor
71 5.9 3.2 4.8 1.8 versicolor
72 6.1 2.8 4.0 1.3 versicolor
73 6.3 2.5 4.9 1.5 versicolor
74 6.1 2.8 4.7 1.2 versicolor
75 6.4 2.9 4.3 1.3 versicolor
76 6.6 3.0 4.4 1.4 versicolor
77 6.8 2.8 4.8 1.4 versicolor
78 6.7 3.0 5.0 1.7 versicolor
79 6.0 2.9 4.5 1.5 versicolor
80 5.7 2.6 3.5 1.0 versicolor
81 5.5 2.4 3.8 1.1 versicolor
82 5.5 2.4 3.7 1.0 versicolor
83 5.8 2.7 3.9 1.2 versicolor
84 6.0 2.7 5.1 1.6 versicolor
85 5.4 3.0 4.5 1.5 versicolor
86 6.0 3.4 4.5 1.6 versicolor
87 6.7 3.1 4.7 1.5 versicolor
88 6.3 2.3 4.4 1.3 versicolor
89 5.6 3.0 4.1 1.3 versicolor
90 5.5 2.5 4.0 1.3 versicolor
91 5.5 2.6 4.4 1.2 versicolor
92 6.1 3.0 4.6 1.4 versicolor
93 5.8 2.6 4.0 1.2 versicolor
94 5.0 2.3 3.3 1.0 versicolor
95 5.6 2.7 4.2 1.3 versicolor
96 5.7 3.0 4.2 1.2 versicolor
97 5.7 2.9 4.2 1.3 versicolor
98 6.2 2.9 4.3 1.3 versicolor
99 5.1 2.5 3.0 1.1 versicolor
100 5.7 2.8 4.1 1.3 versicolor
101 6.3 3.3 6.0 2.5 virginica
102 5.8 2.7 5.1 1.9 virginica
103 7.1 3.0 5.9 2.1 virginica
104 6.3 2.9 5.6 1.8 virginica
105 6.5 3.0 5.8 2.2 virginica
106 7.6 3.0 6.6 2.1 virginica
107 4.9 2.5 4.5 1.7 virginica
108 7.3 2.9 6.3 1.8 virginica
109 6.7 2.5 5.8 1.8 virginica
110 7.2 3.6 6.1 2.5 virginica
111 6.5 3.2 5.1 2.0 virginica
112 6.4 2.7 5.3 1.9 virginica
113 6.8 3.0 5.5 2.1 virginica
114 5.7 2.5 5.0 2.0 virginica
115 5.8 2.8 5.1 2.4 virginica
116 6.4 3.2 5.3 2.3 virginica
117 6.5 3.0 5.5 1.8 virginica
118 7.7 3.8 6.7 2.2 virginica
119 7.7 2.6 6.9 2.3 virginica
120 6.0 2.2 5.0 1.5 virginica
121 6.9 3.2 5.7 2.3 virginica
122 5.6 2.8 4.9 2.0 virginica
123 7.7 2.8 6.7 2.0 virginica
124 6.3 2.7 4.9 1.8 virginica
125 6.7 3.3 5.7 2.1 virginica
126 7.2 3.2 6.0 1.8 virginica
127 6.2 2.8 4.8 1.8 virginica
128 6.1 3.0 4.9 1.8 virginica
129 6.4 2.8 5.6 2.1 virginica
130 7.2 3.0 5.8 1.6 virginica
131 7.4 2.8 6.1 1.9 virginica
132 7.9 3.8 6.4 2.0 virginica
133 6.4 2.8 5.6 2.2 virginica
134 6.3 2.8 5.1 1.5 virginica
135 6.1 2.6 5.6 1.4 virginica
136 7.7 3.0 6.1 2.3 virginica
137 6.3 3.4 5.6 2.4 virginica
138 6.4 3.1 5.5 1.8 virginica
139 6.0 3.0 4.8 1.8 virginica
140 6.9 3.1 5.4 2.1 virginica
141 6.7 3.1 5.6 2.4 virginica
142 6.9 3.1 5.1 2.3 virginica
143 5.8 2.7 5.1 1.9 virginica
144 6.8 3.2 5.9 2.3 virginica
145 6.7 3.3 5.7 2.5 virginica
146 6.7 3.0 5.2 2.3 virginica
147 6.3 2.5 5.0 1.9 virginica
148 6.5 3.0 5.2 2.0 virginica
149 6.2 3.4 5.4 2.3 virginica
150 5.9 3.0 5.1 1.8 virginica
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
> head(iris,3)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
> tail(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
145 6.7 3.3 5.7 2.5 virginica
146 6.7 3.0 5.2 2.3 virginica
147 6.3 2.5 5.0 1.9 virginica
148 6.5 3.0 5.2 2.0 virginica
149 6.2 3.4 5.4 2.3 virginica
150 5.9 3.0 5.1 1.8 virginica
(2)行列数都多的数据框可取前几行前几列查看
> iris[1:3,1:3]
Sepal.Length Sepal.Width Petal.Length
1 5.1 3.5 1.4
2 4.9 3.0 1.4
3 4.7 3.2 1.3
(3) 查看每一列的数据类型和具体内容
> str(df)
'data.frame': 3 obs. of 6 variables:
$ gene: chr "gene1" "gene2" "gene3"
$ sam1: num 0.0746 -1.9894 0.6198
$ sam2: num -0.0561 -0.1558 5
$ sam3: num -0.478 0.418 1.359
$ exp : num 12 23 50
$ abc : num 23 15 37
> str(iris)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
(4)如果列名顺序错乱,如何按照指定顺序重排?
(5)去除含有缺失值的行
生成一个有NA的数据框
> df<-data.frame(X1 = LETTERS[1:5],X2 = 1:5)
> df[2,2] <- NA
> df[4,1] <- NA
> df
X1 X2
1 A 1
2 B NA
3 C 3
4 4
5 E 5
>
> na.omit(df)
X1 X2
1 A 1
3 C 3
5 E 5
(6)两个表格的链接
> test1 <- data.frame(name = c('jimmy','nicker','doodle'),
+ blood_type = c("A","B","O"))
> test1
name blood_type
1 jimmy A
2 nicker B
3 doodle O
> test2 <- data.frame(name = c('doodle','jimmy','nicker','tony'),
+ group = c("group1","group1","group2","group2"),
+ vision = c(4.2,4.3,4.9,4.5))
> test2
name group vision
1 doodle group1 4.2
2 jimmy group1 4.3
3 nicker group2 4.9
4 tony group2 4.5
>
> test3 <- data.frame(NAME = c('doodle','jimmy','lucy','nicker'),
+ weight = c(140,145,110,138))
> tmp =merge(test1,test2,by="name")
> merge(test1,test3,by.x = "name",by.y = "NAME")
name blood_type weight
1 doodle O 140
2 jimmy A 145
3 nicker B 138
矩阵和列表
> m <- matrix(1:9, nrow = 3)
> colnames(m) <- c("a","b","c") #列名
> m
a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> #整行
> m[2,]
a b c
2 5 8
> #整列
> m[,1]
[1] 1 2 3
> #单个格
> m[2,3]
c
8
> #多个格
> m[2:3,1:2]
a b
[1,] 2 5
[2,] 3 6
> #转置和转换
> m
a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> t(m)
[,1] [,2] [,3]
a 1 2 3
b 4 5 6
c 7 8 9
> as.data.frame(m)
a b c
1 1 4 7
2 2 5 8
3 3 6 9
列表
> l <- list(m=matrix(1:9, nrow = 3),
+ df=data.frame(gene = paste0("gene",1:3),
+ sam = paste0("sample",1:3),
+ exp = c(32,34,45)),
+ x=c(1,3,5))
> l
$m
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
$df
gene sam exp
1 gene1 sample1 32
2 gene2 sample2 34
3 gene3 sample3 45
$x
[1] 1 3 5
>
> l[[2]]
gene sam exp
1 gene1 sample1 32
2 gene2 sample2 34
3 gene3 sample3 45
> l$df
gene sam exp
1 gene1 sample1 32
2 gene2 sample2 34
3 gene3 sample3 45
补充:元素的名字
(1)向量
x=1:10
names(x)=letters[1:10]
x
a b c d e f g h i j
1 2 3 4 5 6 7 8 9 10
x["a"]
a
1(2)数据框
df
X1 X2
1 A 1
2 B NA
3 C 3
44
5 E 5
names(df)
[1] "X1" "X2"
df[,"X1"]
[1] "A" "B" "C" NA "E"(3)列表
names(l)
[1] "m" "df" "x"
l[["df"]]
gene sam exp
1 gene1 sample1 32
2 gene2 sample2 34
3 gene3 sample3 45
(4)删除
删除一个
rm(l)
删除多个
rm(df,m)
删除全部
rm(list = ls())