参考教材:《统计建模与R软件》薛毅,陈立萍 编著
2.1 建立一个R文件,在文件中输入变量,
,并作以下运算。
(1)计算,其中
;
(2)计算 x 与 y 的內积;
(3)计算 x 与 y 的外积.
程序脚本:
x <- c(1,2,3)
y <- c(4,5,6)
e <- c(1,1,1)
z <- 2*x+y+e
z
crossprod(x,y)
tcrossprod(x,y)
运行结果:
> x <- c(1,2,3)
> y <- c(4,5,6)
> e <- c(1,1,1)
> z <- 2*x+y+e
> z #求z
[1] 7 10 13
> crossprod(x,y) #内积
[,1]
[1,] 32
> tcrossprod(x,y) #外积
[,1] [,2] [,3]
[1,] 4 5 6
[2,] 8 10 12
[3,] 12 15 18
2.2 将1,2,... ,20构成两个4*5阶矩阵,其中矩阵A是按列输入,矩阵B是按行输入,并作如下运算。
(1);
(2);
(3),其中
;
(4)是由
的前3行和前3列构成的矩阵;
(5)是由矩阵
的各列构成的矩阵,但不含
的第3列.
【第(2)题笔者认为题目有错误,因为两个4*5的矩阵无法直接相乘】
程序脚本:
A <- matrix(1:20,nrow = 4,byrow = FALSE)
B <- matrix(1:20,nrow = 4,byrow = TRUE)
C <- A + B
E <- A * B
F <- A[1:3,1:3]
G <- B[,-3]
C
E
F
G
运行结果:
> A <- matrix(1:20,nrow = 4,byrow = FALSE)
> B <- matrix(1:20,nrow = 4,byrow = TRUE)
> C <- A + B
> E <- A * B
> F<-A[1:3,1:3]
> G<-B[,-3]
> C #(1)
[,1] [,2] [,3] [,4] [,5]
[1,] 2 7 12 17 22
[2,] 8 13 18 23 28
[3,] 14 19 24 29 34
[4,] 20 25 30 35 40
> E #(3)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 10 27 52 85
[2,] 12 42 80 126 180
[3,] 33 84 143 210 285
[4,] 64 136 216 304 400
> F #(4)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
> G #(5)
[,1] [,2] [,3] [,4]
[1,] 1 2 4 5
[2,] 6 7 9 10
[3,] 11 12 14 15
[4,] 16 17 19 20
2.3 构造一个向量x,向量是由5个1,3个2,4个3和2个4构成,注意用到rep()函数.
程序脚本:
x <- rep(1:4,times=c(5,3,4,2))
x
运行结果:
> x <- rep(1:4,times=c(5,3,4,2))
> x
[1] 1 1 1 1 1 2 2 2 3 3 3 3 4 4
2.4 生成一个5阶的Hilbert矩阵,
,
,
(1)计算Hilbert矩阵H的行列式;
(2)求H的逆矩阵;
(3)求H的特征值和特征向量.
【笔者原本的解法较为繁琐,感谢CSDN作者方舟aark提供的思路,帮助笔者简化了代码】
程序脚本:
n = 5
i <- matrix(rep(1:n,each = n), nrow = n)
j <- matrix(rep(1:n,each = n), nrow = n, byrow = T)
H = 1/(i+j-1)
H
det(H)
solve(H)
eigen(H)
运行结果:
> n = 5
> i <- matrix(rep(1:n,each = n), nrow = n)
> j <- matrix(rep(1:n,each = n), nrow = n, byrow = T)
> H = 1/(i+j-1)
> H #生成Hilbert矩阵
[,1] [,2] [,3] [,4] [,5]
[1,] 1.0000000 0.5000000 0.3333333 0.2500000 0.2000000
[2,] 0.5000000 0.3333333 0.2500000 0.2000000 0.1666667
[3,] 0.3333333 0.2500000 0.2000000 0.1666667 0.1428571
[4,] 0.2500000 0.2000000 0.1666667 0.1428571 0.1250000
[5,] 0.2000000 0.1666667 0.1428571 0.1250000 0.1111111
> det(H) #第(1)小题
[1] 3.749295e-12
> solve(H) #第(2)小题
[,1] [,2] [,3] [,4] [,5]
[1,] 25 -300 1050 -1400 630
[2,] -300 4800 -18900 26880 -12600
[3,] 1050 -18900 79380 -117600 56700
[4,] -1400 26880 -117600 179200 -88200
[5,] 630 -12600 56700 -88200 44100
> eigen(H) #第(3)小题
eigen() decomposition
$values
[1] 1.567051e+00 2.085342e-01 1.140749e-02 3.058980e-04 3.287929e-06
$vectors
[,1] [,2] [,3] [,4] [,5]
[1,] -0.7678547 0.6018715 -0.2142136 0.04716181 0.006173863
[2,] -0.4457911 -0.2759134 0.7241021 -0.43266733 -0.116692747
[3,] -0.3215783 -0.4248766 0.1204533 0.66735044 0.506163658
[4,] -0.2534389 -0.4439030 -0.3095740 0.23302452 -0.767191193
[5,] -0.2098226 -0.4290134 -0.5651934 -0.55759995 0.376245545
2.5 已知有5名学生的数据,如表2.3所示,用数据框的形式读入数据.
序号 | 姓名 | 性别 | 年龄 | 身高 /cm | 体重 /kg |
1 | 张三 | 女 | 14 | 156 | 42.0 |
2 | 李四 | 男 | 15 | 165 | 49.0 |
3 | 王五 | 女 | 16 | 157 | 41.5 |
4 | 赵六 | 男 | 14 | 162 | 52.0 |
5 | 丁一 | 女 | 15 | 159 | 45.5 |
程序脚本:
df <- 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.0,49.0,41.5,52.0,45.5)
);df
运行结果:
> df <- 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.0,49.0,41.5,52.0,45.5)
+ );df
Name Sex Age Height Weight
1 ZhangSan F 14 156 42.0
2 LiSi M 15 165 49.0
3 WangWu F 16 157 41.5
4 ZhaoLiu M 14 162 52.0
5 DingYi F 15 159 45.5
2.6 将题2.5中的数据表2.3的数据写成一个纯文本文件,用函数read.table()读该文件,然后再用函数write.csv()写成一个能用Excel表打开的文件,并用Excel表打开.
【本题笔者也是从别的网站东拼西凑写出的答案,不一定正确,欢迎读者来与笔者讨论】
df <- 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.0,49.0,41.5,52.0,45.5)
);df
write.table(df, "twopointthree_data.txt")
df2 <- read.table("twopointthree_data.txt")
write.csv(df2, "twopointthree_data.csv")
2.7 编写一个R程序(函数). 输入一个整数n,如果n0,则终止运算,并输出一句话:“要求输入一个正整数”;否则,如果n是偶数,则将n除2,并赋值给n;否则,将3n+1赋给n. 不断循环,直到n=1,才停止运算,并输出一句话:“运算成功”. 这个例子是为了验证数论中的一个简单定理.
程序脚本:
func1 <- function(a) {
if (a <= 0)
list(fail = "要求输入一个正整数")
else{
repeat {
if (a == 1) break
if (a %% 2 == 0) {
a = a / 2
} else {
n = 3 * a + 1
}
}
list(succ = "运算成功")
}
}
#测试1
func1(2)
#测试2
func1(-1)
运行结果:
> func1 <- function(a) {
+ if (a <= 0)
+ list(fail = "要求输入一个正整数")
+ else{
+ repeat {
+ if (a == 1) break
+ if (a %% 2 == 0) {
+ a = a / 2
+ } else {
+ n = 3 * a + 1
+ }
+ }
+ list(succ = "运算成功")
+ }
+ }
> #测试1
> func1(2)
$succ
[1] "运算成功"
> #测试2
> func1(-1)
$fail
[1] "要求输入一个正整数"
(个人答案,如有错误或更正建议欢迎联系作者)