此次作业选择的是鸢尾花数据,因为可以从r语言预先设置的数据集中提取,所以读入数据这里我不做代码书写,不使用read.csv(),直接使用data()命令获得数据集 # 读入数据
data(iris)
# 如果为外部数据可以使用
# read.csv()
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
使用for循环,实现对iris数据集各个列的均值求解
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
# 建立空向量存储结构
result_mean<-c()#
for(i in 1:4){
a<-mean(iris[,i])
print(a)# 打印
result_mean<-c(result_mean,a)
}
## [1] 5.843333
## [1] 3.057333
## [1] 3.758
## [1] 1.199333
result_mean
## [1] 5.843333 3.057333 3.758000 1.199333
使用ggplot绘制iri数据集的散点图、箱式图
library(ggplot2)#get the packages
# plot the san dian tu
ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,group=Species,colour=Species))+
geom_point()
可以看出根据两个指标无法区分三类的鸢尾花 ## 看看这四个指标有没有差异,可视化箱式图
library(reshape2)
iris_long<-melt(iris,id.var= "Species")
head(iris_long)
## Species variable value
## 1 setosa Sepal.Length 5.1
## 2 setosa Sepal.Length 4.9
## 3 setosa Sepal.Length 4.7
## 4 setosa Sepal.Length 4.6
## 5 setosa Sepal.Length 5.0
## 6 setosa Sepal.Length 5.4
ggplot(iris_long,aes(x=factor(variable),y=value,fill=Species))+
geom_boxplot()+
facet_wrap(~variable,scale="free")# 试试分面
关于ggplot绘制两个图,比R语言自带的绘制好很多,love&peace
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有