library(datasets)
head(cars)
plot(cars)
|
|
plot(cars$dist,cars$speed,xlab = "dist", ylab = "speed",xlim = c(0,20)) |
|
attach(cars) plot(dist,speed,xlab = "dist",ylab = "speed") |
|
plot(cars$dist,cars$speed,xlab = "dist",ylab = "speed",main = "cars 散点图",type="b",pch=4,lwd=2,col="red") |
|
a<-c(20,30,40,50,60)
b<-c(11,22,33,44,66)
c<-c(15,25,35,45,55)
plot(a,b,type = "b")#有点,用线连接
plot(a,b,type = "o")#有点,线连接,线过点
plot(a,b,type = "p")#点图
plot(a,b,type = "h")#用线表示值
plot(a,b,type = "l")#直接线链接
plot(a,b,type = "s")#梯形图
plot(a,b,type = "S")#梯形图
plot(a,b,type = "n")#不做图
|
|
plot(a,b,pch=0)
plot(a,b,pch=1)
plot(a,b,pch=2)
plot(a,b,pch=3)
plot(a,b,lty=2,lwd=2,pch=15,cex=2)
plot(a,b,type="b",lty=2,lwd=2,pch=15,cex=2)
|
|
install.packages("RColorBrewer")
library(RColorBrewer)
plot(a,b,type="b",lty=2,lwd=2,pch=15,cex=2,col=2)
opar<-par(no.readonly = FALSE)
par(pin=c(2,3))
par(lwd=2,cex=1.5)
par(cex.axis=.75,font.axis=3)
|
|
plot(a,b,type="b",pch=19,lty=2,col=2) |
|
plot(a,b,type="b",pch=23,lty=5,col=2,bg="green") par(opar) |
|
par(mfrow = c(2, 2))
plot(a,c,type = "o",col=3,lty=3,pch=3,lwd=4,
main = "实验图形",xlab="a",ylab="c",
xlim = c(0,100),ylim=c(0,100))
x<-c(1:15)
y<-x
z<-10/x
opar<-par(no.readonly = TRUE)
par(mar=c(5,4,4,8)+0.1)
plot(x,y,type = "b",pch=21,col=2,yaxt="n",lty=3,ann = FALSE)
lines(x,z,type = "b",pch=3,col=4,lty=2)
axis(side = 2,at=x,labels = x,col.axis=2,las=2)
axis(side = 4,at=z,labels = round(z,digits = 2),
cex.axis=2,las=2)
par(opar)
|
|
library(datasets)#加载数据包
attenu#
attach(attenu)
subset(attenu,dist>=40)#选出dist列大于等于14的一行
df<-subset(attenu,event=="2" & dist=="107",select=c(dist));df#显示特定的行
s=subset(attenu,event=="2" & dist=="107",select=c(event,dist));s#多条件查询
df1<-as.list(attenu);df1#转换成列表
append(df1$event,123)#在event后部分追加123这个数
append(df1,1235,after = 55)#指定长度追加
data1= data.frame(Id=c(1:6),Product=c(rep("Toaster",3),rep("Radio",3)))
data2 = data.frame(Id=c(2,4,6,7),State=c(rep("Alabama",3),rep("Ohio",1)))
merge(data1,data2,all=T)#取两数据框并集
merge(data1,data2,all = F)#取交集
merge(data1,data2,all=FALSE)#只是列先后的区别
merge(data1, data2, all.x=TRUE)#以df1所拥有的显示
merge(data1,data2)
#有两个及以上相同的列,合并则会自动选择两个均相同的,
#结果同merge(df1, df2, by = c("id", "sex"))
merge(data1,data2,by="Id")#两数据框中共有的sex那一列则会以sex.x和sex.y形式输出。
#aggregate()函数
|
|
#aggregate()函数
mtcars#R语言自带的汽车数据集
attach(mtcars)
str(mtcars)#查看数据集的列名
aggregate(mtcars,by=list(cyl,gear),FUN = mean)#使用aggregate函数按cyl(汽缸数)进行分类计算平均值
#公式是一种特殊的R数据对象,在aggregate函数中使用公式参数可以对数据框的部分指标进行统计
aggregate(cbind(mpg,hp) ~ cyl+gear, FUN=mean)
#频次统计table
table(hp)#统计mtcars数据中的hp列中出现结果频次
#函数apply(),list,matrix,array三种形式均可以
rname = c("r1","r2","r3")
cname = c("c1","c2")
b<-matrix(1:6,nrow=3, dimnames = list(rname, cname))
apply(b,1,sum)#1—表示按行计算,2—按列计算;
apply(b,2,sum)#1—表示按行计算,2—按列计算;
m<-c("SPYDERMAN","BATMAN","VERTIGO","CHINATOWN")
m1<-lapply(m,tolower)#把大写的字符改成小写
m1
sapply(mtcars,min)#查找数据框中最小的
tapply(mtcars$mpg, INDEX=mtcars$mpg, FUN = mean)#计算数据框中的列或者向量里的均值
|
|