原作者: wgdmla是我
链接: https://www.Jian Shu.com/p/2 a1 a2 ca 1a 916
来源:简单书
版权归作者所有。 商业转载请联系作者取得许可。 非商业转载请注明出处。
只用于个人学习和交流,如入侵删除!
1.5种基础数据结构,4种常见数据类型
五种基本数据结构,四种常见数据类型
基础数据结构介绍
1 .向量(c ) (一维数组) )。
向量图像
A
A
[1] 1 2 3 4 5 6
B
B
[1] 'one' 'two' 'three '
C
C
[1]真假真假
a[2]。
[1] 2
a[-2]
[1] 1 3 4 5 6
a[c(2:4 ) ]
[1] 2 3 4
A
[1] 1 2 3 4 5 6
2 .矩阵(matrix ) )二维数组)矩阵
mymat
mymat
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
mymat[2,]
[1] 6 7 8 9 10
mymat[,2]
[1] 2 7
my mat [ 1,5 ]
[1] 5
3 .数组(array )更高维的数组)数组
myarr
myarr
、1
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
、2
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
myarr[1,]
[,1] [,2]
[1,] 1 13
[2,] 4 16
[3,] 7 19
[4,] 10 22
myarr [ 1,2,]
[1] 4 16
myarr [ 1,2,1 ]
[1] 4
4 .数据框(data.frame ) (具有不同类型的数组) )数据框
数据框示例
名字
age
df
df
name age
1小明19
2小虹20
df[1,]
name age
1小明19
df[,2]
[1] 19 20
df [ 1:2,1:2 ]
name age
1小明19
2小虹20
df$name
[1]肖明红
levels :小洪晓
是Rownames(df )
[1] '1' '2'
colnames(df )
[1] 'name' 'age '
因子变量(向量的因子化) )
类别数据对分组数据的研究非常有用。 (男女,高中低) )
r的系数变量类似于类别数据。
状态
状态
状态
索引
plot数据
是attach (打印数据)
thefollowingobjectsaremasked _ by _.globalenv :
p>index, status> boxplot(index~status,col="red")
运行结果
类别变量,有序变量称为因子,决定了数据的分析方式和视觉呈现形式。
Attach()可以将数据框添加到R的搜索路径中,当R遇到一个变量名后,将检测搜索路径中的数据框,定位这个变量。
5. 列表
一种简单的方式组织和调用不相干的信息。
R函数的许多运行结果都是以列表的形式返回。
> lis
+ wife='怕黑的秀发',
+ no.children=3,
+ child.ages=c(4,7,9))
> lis
$name
[1] "fred"
$wife
[1] "怕黑的秀发"
$no.children
[1] 3
$child.ages
[1] 4 7 9
> lis$name
[1] "fred"
> lis[1]
$name
[1] "fred"
常用的函数:
head(object):查看对象的开始部分。
tail(object):查看对象的结尾部分。
str(object):显示对象的结构。
length(object):显示对象的元素或成分个数。
dim(object):显示对象的维度,针对矩阵/数据框。
mode(object):显示对象的类型。(numeric/logical/character/list等)
t(object):转置对象。
names(object):显示对象各成分的名称。
fix(object):直接编辑对象。
newobject
rbind():将两个或以上的对象按行合并。
cblind():将两个或以上的对象按列合并。
> head(lis)
$name
[1] "fred"
$wife
[1] "怕黑的秀发"
$no.children
[1] 3
$child.ages
[1] 4 7 9
> tail(lis)
$name
[1] "fred"
$wife
[1] "怕黑的秀发"
$no.children
[1] 3
$child.ages
[1] 4 7 9
> str(lis)
List of 4
$ name : chr "fred"
$ wife : chr "怕黑的秀发"
$ no.children: num 3
$ child.ages : num [1:3] 4 7 9
> length(lis)
[1] 4
> dim(lis)
NULL
> mode(lis)
[1] "list"
> t(lis)
name wife no.children child.ages
[1,] "fred" "怕黑的秀发" 3 Numeric,3
> names(lis)
[1] "name" "wife" "no.children" "child.ages"fix(lis)之后会弹出一个编辑器的对话框
R的流程控制
if表达式的写法:
if(条件) 表达式
if(条件) 表达式1 else 表达式2if表达式的用法
> if (p <= 0.05){
+ print("p<=0.05")
+ }else{
+ print("p>0.05")
+ }
[1] "p>0.05"
循环的写法循环for、while
> i=0
> while(i<10){
+ print(i)
+ i
+ }
[1] 0
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
next:循环控制(相当于continue)next循环控制
break:循环控制(相当于break)
break循环控制
> for (i in v){
+ if(i=='D'){
+ next}
+ print(i)}
[1] "A"
[1] "B"
[1] "C"
[1] "E"
[1] "F"
> for (i in v){
+ if(i=='D'){
+ break}
+ print(i)}
[1] "A"
[1] "B"
[1] "C"
R语言的函数
> rcal
+ {
+ z
+ result
+ result
+ }
> rcal(3,4)
[1] 5
数据读入与读出(略)数据读入与读出
数据清理(略)
数据清理的相关包
数据可视化
> x = c(1,2,3,4,5,6)
> y = c(5,2,7,4,8,1)
> plot(x,y)plot
> barplot(x)barplot
> plot(x,y,cex = c(1,3),type = 'p',pch = 19,col = 'blue',cex.axis = 1.5, col.axis = 'darkgreen', font.axis = 2,main = '这是主标题:plot初试',font.sub = 3, cex.sub = 1.5, col.sub = 'red',xlab = '这是x轴标签', ylab = '这是y轴标签', cex.lab = 1.5, font.lab = 2, col.lab = 'grey20',xlim = c(0,3), ylim = c(0,7))
> abline(h=2, v=3, lty=1:2, lwd=2,col="red")
> legend("topright", legend="我是图例n我在这儿",text.col="red", text.width=0.5)plot函数的一些变式
图形参数:
符号和线条:pch、cex、lty、lwd
颜色:col、col.axis、col.lab、col.main、col.sub、fg、bg
文本属性:cex、cex.axis、cex.lab、cex.main、cex.sub、font、font.axis、font.lab、font.main、font.sub
文本添加、坐标轴的自定义和图例:
title()、main、sub、xlab、ylab、text()
axis()、abline()
legend()
多图绘制时候,可使用par()设置默认的图形参数
par(lwd=2, cex=1.5)
图形参数设置:
par(optionname=value,…)
par(pin=c(width,height)) 图形尺寸
par(mfrow=c(nr,nc)) 图形组合,一页多图
layout(mat) 图形组合,一页多图
par(mar=c(bottom,left,top,right)) 边界尺寸
par(fig=c(x1,x2,y1,y2),new=TURE) 多图叠加或排布成一幅图par函数完成多个图片放在一起
频数分布直方图:
> hist(seqlength,breaks=100,col="red",freq=FALSE,main="Histogram with dengsity curve",ylab="Density", xlab="Sequence length")
> lines(density(seqlength),col="blue4",lwd=2)频数分布直方图的例子
> relative
> taxon
> ratio
> ratio
> label
> pie(relative,labels=label, main="ITS1-Sample S1", radius=1,col=rainbow(length(label)),cex=1.3)
> library(plotrix)
> fan.plot(relative,labels=label,main="Fan plot")
> pie3D(relative,labels=label, height=0.2, theta=pi/4, explode=0.1, col=rainbow(length(label)), border="black",font=2,radius=1,labelcex=0.9)
图片的导出:
pdf(file="file.pdf", width=7, height=10)
png(file="file.png",width=480,height=480)
jpeg(file="file.png",width=480,height=480)
tiff(file="file.png",width=480,height=480)
dev.off()