<strong><span style="font-size:32px;">Ex1</span></strong>
<span style="color:#33cc00;"># programe: cha4.6 ex1 <img src="https://img-blog.csdn.net/20151226122133602?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" align="right" alt="" />
# 12-25-15 @author Sigua
# file path</span>
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"
<span style="color:#33cc00;"># read_in data</span>
data_age<-read.table(file_age,header = TRUE)
data_teeth<-read.table(file_teeth,header = TRUE)
<span style="color:#33cc00;"># cbind</span>
age_teeth<-cbind(data_age,data_teeth$Num_Teeth)
<span style="color:#33cc00;"># write_out data</span>
write.table(age_teeth,file = "F:/R/rWorkPlace/age_teeth.txt")
<strong>参考别人</strong>
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)
<pre name="code" class="plain">
<strong><span style="font-size:32px;">Ex2</span></strong>
<span style="background-color: rgb(255, 255, 255);"><span style="color:#33cc00;"># programming :cha4.6ex2
# "Fri Dec 25 16:21:28 2015" @author:Sigua
# clear</span></span>
rm(list=ls())<img src="https://img-blog.csdn.net/20151226122649791?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" align="right" alt="" />
<span style="color:#33cc00;"># read_in data 读出结果是字符型向量</span>
file_name<-"F:/R/rWorkPlace/age_teethex.txt"
age_teeth_data<-scan(file=file_name,what="")
<span style="color:#33cc00;">#打印标题行</span>
cat(format(age_teeth_data[1],width=4),
format(age_teeth_data[2],width=4),
format(age_teeth_data[3],width=8),"\n",seq="")
<span style="color:#33cc00;">#将余下数据转化为10*3的矩阵</span>
len1<-length(age_teeth_data)
data_matrix<-matrix(as.numeric(age_teeth_data[4:len1]),ncol=3,nrow=10,byrow=TRUE)
<span style="color:#33cc00;">#排序第二列 返回从小到大顺序的序列号</span>
y<-order(data_matrix[,2])
len2<-len1%/%3-1
<span style="color:#33cc00;">#分别打印每一行 按照第二列顺序 </span>
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="")
<p><pre name="code" class="plain"><strong><span style="font-size:32px;">Ex3</span></strong>
<span style="color:#33cc00;"># programming :cha4.6ex3
# "Fri Dec 25 16:38:51 2015" @author Sigua<img src="https://img-blog.csdn.net/20151226122850087?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" align="right" alt="" /></span>
<span style="color:#33cc00;">#清除当前空间的对象</span>
rm(list=ls())
<span style="color:#33cc00;">#打印表头</span>
cat(format("number",width=10),format("square",width=10),format("cube",width=10),"\n",seq="")
<span style="color:#33cc00;">#打印每一行</span>
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="")
}
<strong><span style="font-size:32px;">Ex4</span></strong>
<span style="color:#33cc00;"># programming :cha4.6 ex4
# "Fri Dec 25 16:53:08 2015" @author Sigua
# 清除空间对象<img src="https://img-blog.csdn.net/20151226123031877?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" align="right" alt="" /></span>
rm(list = ls())
<span style="color:#33cc00;"># 计算</span>
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)
<strong>参考别人</strong>
rm(list = ls())
n <- 1:9
<span style="color:#33cc00;">#数学中的矩阵相乘</span>
mtable <- n %*% t(n)
show(mtable)
<strong><span style="font-size:32px;">Ex5</span></strong>
<span style="color:#33cc00;"># cha4.6ex5
# "Sat Dec 26 12:13:02 2015" @author Sigua
# clear对象</span>
rm(list = ls())<img src="https://img-blog.csdn.net/20151226123220173?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" align="right" alt="" />
<span style="color:#33cc00;"># 定义自变量取值范围 (x<-1 || x>1)</span>
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)
<span style="color:#33cc00;"># 计算函数y值</span>
y1<-sqrt(3*(x1^2-1))
y2<-sqrt(3*(x2^2-1))
<span style="color:#33cc00;"># 画出框架(大小 范围)</span>
plot(x1,y1,xlim=c(-5,5),ylim=c(-9,9),type="n",xlab="x",ylab="y")
<span style="color:#33cc00;"># 在框架上作图 (lines()只能用在plot之后</span>
lines(x1,y1)
lines(x1,-y1)
lines(x2,y2)
lines(x2,-y2)
lines(xx, sqrt(3) * xx)
lines(xx, -sqrt(3) * xx)
<span style="color:#33cc00;"># 添加解释性元素</span>
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)
}
}