chapter 4
***Ex1***
# programe: cha4.6 ex1 ![Ex1](https://img-blog.csdn.net/20151226125117523)
# 12-25-15 @author Sigua
# file path
file_age<-"F:/R/R-3.2.2/library/spuRs/resources/data/age.txt"
file_teeth<-"F:/R/R-3.2.2/library/spuRs/resources/data/teeth.txt"
# read_in data
data_age<-read.table(file_age,header = TRUE)
data_teeth<-read.table(file_teeth,header = TRUE)
# cbind
age_teeth<-cbind(data_age,data_teeth$Num_Teeth)
# write_out data
write.table(age_teeth,file = "F:/R/rWorkPlace/age_teeth.txt")
***参考别人***
file_name1 = "F:/R/R-3.2.2/library/spuRs/resources/data/age.txt"
data1 <- scan(file = file_name1, what = "")
write(data1, file = "", ncolumns = 2)
file_name2 = "F:/R/R-3.2.2/library/spuRs/resources/data/teeth.txt"
data2 <- scan(file = file_name2, what = "")
write(data2, file = "", ncolumns = 2)
for (i in 1:(length(data1)%/%2)){
write(rbind(format(data1[2*i-1], width = 4),
format(data1[2*i], width = 4),
format(data2[2*i], width = 4)),
ncolumns = 3,
file = "age_teethex.txt",
append = TRUE)
}
***Ex2***
# programming :cha4.6ex2![ex2](https://img-blog.csdn.net/20151226125716430)
# "Fri Dec 25 16:21:28 2015" @author:Sigua
# clear
rm(list=ls())
# read_in data 读出结果是字符型向量
file_name<-"F:/R/rWorkPlace/age_teethex.txt"
age_teeth_data<-scan(file=file_name,what="")
#打印标题行
cat(format(age_teeth_data[1],width=4),
format(age_teeth_data[2],width=4),
format(age_teeth_data[3],width=8),"\n",seq="")
#将余下数据转化为10*3的矩阵
len1<-length(age_teeth_data)
data_matrix<-matrix(as.numeric(age_teeth_data[4:len1]),ncol=3,nrow=10,byrow=TRUE)
#排序第二列 返回从小到大顺序的序列号
y<-order(data_matrix[,2])
len2<-len1%/%3-1
#分别打印每一行 按照第二列顺序
for(i in 1:len2){
cat(format(data_matrix[y[i],1],width=4),
format(data_matrix[y[i],2],width=4),
format(data_matrix[y[i],3],width=4),"\n",seq="")
}
***Ex3***
# programming :cha4.6ex3![ex3](https://img-blog.csdn.net/20151226125744867)
# "Fri Dec 25 16:38:51 2015" @author Sigua
#清除当前空间的对象
rm(list=ls())
#打印表头
cat(format("number",width=10),format("square",width=10),format("cube",width=10),"\n",seq="")
#打印每一行
num<-1:7
for (i in 1:length(num)){
cat(format(num[i],width=10),format(num[i]^2,width=10),format(num[i]^3,width=10),"\n",seq="")
}
***Ex4***
# programming :cha4.6 ex4![ex4](https://img-blog.csdn.net/20151226125802767)
# "Fri Dec 25 16:53:08 2015" @author Sigua
# 清除空间对象
rm(list = ls())
# 计算
x<-1:9
for(j in 2:9){
x<-cbind(x,j*c(1:9))
}
matrix_x<-matrix(x,ncol=9,nrow=9,byrow = TRUE)
print(matrix_x)
***参考别人***
rm(list = ls())
n <- 1:9
#数学中的矩阵相乘
mtable <- n %*% t(n)
show(mtable)
***Ex5***
# cha4.6ex5![ex5](https://img-blog.csdn.net/20151226125818242)
# "Sat Dec 26 12:13:02 2015" @author Sigua
# clear对象
rm(list = ls())
# 定义自变量取值范围 (x<-1 || x#1)
xx<-seq(from=-5,to=5,by=001)
x1<-seq(from=1,to=5,by=0.01)
x2<-seq(from=-5,to=-1,by=0.01)
# 计算函数y值
y1<-sqrt(3*(x1^2-1))
y2<-sqrt(3*(x2^2-1))
# 画出框架(大小 范围)
plot(x1,y1,xlim=c(-5,5),ylim=c(-9,9),type="n",xlab="x",ylab="y")
# 在框架上作图 (lines()只能用在plot之后
lines(x1,y1)
lines(x1,-y1)
lines(x2,y2)
lines(x2,-y2)
lines(xx, sqrt(3) * xx)
lines(xx, -sqrt(3) * xx)
# 添加解释性元素
points(2,0)
text(2,0,"focus(2,0)",pos=4)
title("The hyperbola x^2-y^2/3=1")
points(-2,0)
text(4,sqrt(3)*4,"asymptote y=sqrt(3)*x",pos = 2)
R语言的科学编程与仿真 chapter 4 答案
最新推荐文章于 2023-05-11 16:36:58 发布