# exercise * 2.1X=C (Y=C) (4,5,6) E=c (Rep (1,3)) Z=2*x+y+e;zx%*%y # if x, y as the answer is defined as a matrix, you cannot use%*% because the dimension does not correspond, X%o%y # answer X<-matrix (1:3,nrow=3) Y<-matrix (4:6,nrow=3) E<-matrix (C (1,1,1), nrow=3) Z<-2*x+y+e;zcrossprod (x, y) #内积outer (x, y) #外积 # 2.2a=matrix (1:20,4,5); Ab=matrix (1:20,4,5,byrow=t); Bc=a+b; Cd=crossprod (A, b);D # D=a%*%b;d Wrong dimension does not correspond, the problem is wrong, it is impossible to abe=a*b; Ef=a[1:3,1:3]; Fg=b[,c (1,2,4,5)]; G # g<-b[1:4,-3]; G Note-3 usage # answer A<-matrix (1:20,nrow=4) B<-matrix (1:20,nrow=4,byrow=t) c<-a+b; c#d<-ab# does not exist in the AB notation e<-a*b; ef<-a[1:3,1:3]; fg<-b[1:4,-3]; g# 2.3x=c (Rep (1,5), rep (2,3), rep (3,4), Rep (4,2)), x# answer ditto # 2.4hilbert=function (n=5) {H=matrix (0,nrow=n,ncol=n) for (I in 1:N) {for (J in 1:n) h[i,j]=1/(i+j-1)} H # return value, List (Hmatrix=h,det=det (h), Sol=solve (h), Eig=eigen (h)) # Multivariate return value} H=hilbert (5); Hdet (h) Solve (h) eigen (h) # learned function, make the following modifications to the above funch=function (n=5) {H=matrix (0,n,n) for (I-in 1:n) {1:n] =1/(i+j-1)}}H # return value, enter H # list (Hmatrix=h,det=det (h), Sol=solve (h), Eig=eigen (h)) # can alsoOutput multiple return values as a list, and can define element names for each element # such as list (HILBERT=H) list (hilbert=h)}funch () #2.4 Answer 5 price Hilbert matrix N<-5x<-array (0,dim=c ( N,n)) for (I-in 1:n) {x[i,j]<-1/(i+j-1)}}xdet (x) #矩阵行列式solve (x) #逆矩阵eigen (x) #特征值与特征向量 # 2.5 Data.frame ( ) studentdata=data.frame (' Name ' =c (' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu ', ' ding '), ' Sex ' =c (' female ', ' male ', ' female ', ' male ', ' female '), ' age ' =c (14,15,1 6,14,15), ' Height ' =c (156,165,157,162,159), ' Weight ' =c (42,49,41.5,52,45.5)) #studentdata =data.frame (' Serial number ' =1:5, ' last name Name ' =c ' (' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu ', ' ding '), # ' sex ' =c (' female ', ' male ', ' female ', ' male ', ' female '), ' age ' =c (14,15,16,14,15), # ' Height ' =c (156,165,157,162,159), ' Weight ' =c (42,49,41.5,52,45.5) # No need to enter the serial number. Write.table (Studentdata, ' re2.5.txt ') # answer Student<-data.frame (name=c ("Zhangsan", "Lisi", "Wangwu", "Zhaoliu", " Dingyi "), Sex=c (" F "," M "," F "," M "," F "), Age=c (" 14 "," 15 "," 16 "," 14 "," 15 "), Height=c ("156", "165", "157", "162", "159"), Weight=c ("42", "49", "41.5", "52 "," 45.5 ")) # 2.6th=read.table (' Re2.5.txt ', header=t) write.csv (St, ' re2.5.csv ') #2.6 Answer student1<-write.table ( Student,file= "Student1.txt") student2<-read.table ("Student1.txt", head=t) Student3<-write.csv (student2,file = "Student3.csv") # 2.7func=function (n) {if (n<=0) "requires entering a positive integer" else {while (n!=1) {if (n%%2==0) N=N/2 E LSE N=3*N+1} "operation succeeded"}} # R defined function, no special return value setting, equivalent to code copy and paste, func (8) #返回值为代码的输出值 # answer #2.7func<-function (n) {if (n<=0) L IST ("Requires a positive integer") else{repeat{if (n==1) break else if (n%%2==0) {N<-N/2} else n<-3*n+1} list ("Op Successful ")}}
Statistical modeling and R software exercises two answers