【R语言】期末考试五道题

##question1###
setwd(“G:/Rexam20174710426”)
a<-2
b<-0
c<-1
d<-7
e<-4
f<-7
g<-1
h<-0
i<-4
j<-2
k<-6
o<-NA#12个变量
var<-c(a,b,c,d,e,f,j,h,i,j,k,o)#合并变量
var
sum.var<-sum(var,na.rm=TRUE)
sum.var
mean.var<-mean(var,na.rm=TRUE)
mean.var
length.var<-length(var)
length.var

###question2###
setwd(“G:/Rexam20174710426”)
MyIDmatrix<-matrix(nrow=4,ncol=3)
MyIDmatrix[,1]<-c(2,0,1,7)
MyIDmatrix[,2]<-c(4,7,1,0)
MyIDmatrix[,3]<-c(4,2,6,NA)#给矩阵赋值
colnames(MyIDmatrix)<-c(“Year”,“Area”,“Number”)#给矩阵加名字
MyIDmatrix
write.table(MyIDmatrix,file=“MyIDmatrix.txt”)#写出文件
class(MyIDmatrix)
MyIDdf<-read.table(file=“MyIDmatrix.txt”,header = T)#读入文件
class(MyIDdf)
names(MyIDdf)
str(MyIDdf)#查看属性
MyIDdf
###question3###
setwd(“G:/Rexam20174710426”)
MyIDdfR3<-rbind(MyIDdf,MyIDdf,MyIDdf)
MyIDdfR3
dim(MyIDdfR3)#12行3列数据框
MyIDdfR3[3,3]<-5
MyIDdfR3[11,3]<-7#变成我前面同学,我,我后面同学的学号
MyIDdfR3#新数据框
MyIDdfR3 r o w s u m &lt; − r o w S u m s ( M y I D d f R 3 , n a . r m = T R U E ) M y I D d f R 3 &lt; − M y I D d f R 3 [ o r d e r ( M y I D d f R 3 rowsum&lt;-rowSums(MyIDdfR3,na.rm=TRUE) MyIDdfR3&lt;-MyIDdfR3[order(MyIDdfR3 rowsum<rowSums(MyIDdfR3,na.rm=TRUE)MyIDdfR3<MyIDdfR3[order(MyIDdfR3rowsum),]
MyIDdfR3
###question4###
setwd(“G:/Rexam20174710426”)
is.na(MyIDdfR3[-1,-1])#这个返回的是布尔向量
which(is.na(-MyIDdfR3),arr.ind = TRUE)#找出NA所在行列,共有3个
###question5###
setwd(“G:/Rexam20174710426”)
data()#列出所有可用数据集
View(cars)#直接看表
names(cars)
str(cars)
pdf.options(family=“GB1”)
pdf(file=“YourID.pdf”)
op<-par(mfrow=c(1,2),mar=c(5,5,3,2))#设置面板

#左图
plot(x=cars s p e e d , y = c a r s speed,y=cars speed,y=carsdist,xlab=“Speed”,ylab=“Distance”,pch=16)#实心点
#M.Loess<-loess(dist~speed,data=cars)
#Fit<-fitted(M.Loess)
#Ord1<-order(carsKaTeX parse error: Expected 'EOF', got '#' at position 8: speed) #̲lines(carsspeed[Ord1],Fit[Ord1],lwd=2,lty=1)#拟合回归线
abline(lm(dist~speed,data=cars), lwd=3, col=1)#线性回归

#右图
cars l e i b i e &lt; − &quot; &quot; c a r s leibie&lt;-&quot;&quot; cars leibie<""carsleibie[which(carsKaTeX parse error: Expected 'EOF', got '&' at position 10: speed>=0 &̲ carsspeed<=10)]=“L”
cars l e i b i e [ w h i c h ( c a r s leibie[which(cars leibie[which(carsspeed>10 & cars s p e e d &lt; = 20 ) ] = &quot; M &quot; c a r s speed&lt;=20)]=&quot;M&quot; cars speed<=20)]="M"carsleibie[which(carsKaTeX parse error: Expected 'EOF', got '&' at position 10: speed>20 &̲ carsspeed<=30)]=“H”
cars l e i b i e &lt; − f a c t o r ( c a r s leibie&lt;-factor(cars leibie<factor(carsleibie,c(“L”,“M”,“H”))#需要设置因子水平
boxplot(dist~leibie,data=cars,xlab=“Speed”,ylab=“Distance”)
par(op)#关闭par
dev.off()
pdf.options(reset = TRUE)
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值