R 学习笔记1

函数c(),length(),mode(),rbind(),cbind()

x=c(1,3,4,2,5,6,3,2)
x
[1] 1 3 4 2 5 6 3 2

mode(x)
[1] “numeric”
y=c(“a”,”d”,”xra”)
y
[1] “a” “d” “xra”
mode(y)
[1] “character”

x1=c(2,4,6,9,0)
x2=c(1,3,5,7,9)
length(x1)
[1] 5
length(x2)
[1] 5
mode(x1)
[1] “numeric”
x1
[1] 2 4 6 9 0
x1[3]
[1] 6
a1=c(1:100)
length(a1)
[1] 100
a1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100
rbind(x1,x2)
[,1] [,2] [,3] [,4] [,5]
x1 2 4 6 9 0
x2 1 3 5 7 9
cbind(x1,x2)
x1 x2
[1,] 2 1
[2,] 4 3
[3,] 6 5
[4,] 9 7
[5,] 0 9
m1=rbind(x1,x2)
m1
[,1] [,2] [,3] [,4] [,5]
x1 2 4 6 9 0
x2 1 3 5 7 9

函数mean(),sum(),min(),max(),var(),sd(),prod()
var() 求方差
sd() 标准差
prod() 连乘

x=c(1:100)
mean(x)
[1] 50.5
sum(x)
[1] 5050
max(x)
[1] 100
min(x)
[1] 1
var(x)
[1] 841.6667
var(X)
错误于is.data.frame(x) : 找不到对象’X’
prod(x)
[1] 9.332622e+157
sd(x)
[1] 29.01149

6

help(mode)
starting httpd help server … done
1:10
[1] 1 2 3 4 5 6 7 8 9 10
1:10-1
[1] 0 1 2 3 4 5 6 7 8 9
1:10*2
[1] 2 4 6 8 10 12 14 16 18 20
a=1:10
a
[1] 1 2 3 4 5 6 7 8 9 10
a-1
[1] 0 1 2 3 4 5 6 7 8 9
2:60*2
[1] 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
[20] 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78
[39] 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116
[58] 118 120
2:60*2+1
[1] 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41
[20] 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79
[39] 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117
[58] 119 121
a=2:60*+1
a[5]
[1] 6
a[1]
[1] 2
a[-5]
[1] 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
[26] 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
[51] 53 54 55 56 57 58 59 60
a
[1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
[26] 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
[51] 52 53 54 55 56 57 58 59 60

a[1:5]
[1] 2 3 4 5 6
a[-(1:5)]
[1] 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
[26] 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
[51] 57 58 59 60
a[1,2,3]
错误于a[1, 2, 3] : 量度数目不对
a[c(2,4,7)]
[1] 3 5 8
a[3:8]
[1] 4 5 6 7 8 9
a[a<20]
[1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
a[a>30 & a<50]
[1] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
a[a[3]]
[1] 5

seq 函数 ,
seq(5,20)
[1] 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
seq(5,121,by=6)
[1] 5 11 17 23 29 35 41 47 53 59 65 71 77 83 89 95 101 107 113
[20] 119
seq(5,121,by=6,length=10)
错误于seq.default(5, 121, by = 6, length = 10) : 太多参数
seq(5,121,length=10)
[1] 5.00000 17.88889 30.77778 43.66667 56.55556 69.44444 82.33333
[8] 95.22222 108.11111 121.00000

产生字母序列 seq

letters[1:30]
[1] “a” “b” “c” “d” “e” “f” “g” “h” “i” “j” “k” “l” “m” “n” “o” “p” “q” “r” “s”
[20] “t” “u” “v” “w” “x” “y” “z” NA NA NA NA

a=c(2,3,4,2,5,1,6,3,2,5,8,5,7,3)
a
[1] 2 3 4 2 5 1 6 3 2 5 8 5 7 3
which.max(a)
[1] 11
which.min(a)
[1] 6
a[which.max(a)]
[1] 8
which(a==2)
[1] 1 4 9
a[which(a==2)]
[1] 2 2 2
which(a>5)
[1] 7 11 13
a[which(a>5)]
[1] 6 8 7

rev()函数,sort()函数

a=1:20
rev(a)
[1] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3
[19] 2 1
a
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20

sort(a)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20
rev(sort(a))
[1] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3
[19] 2 1

错误: 找不到对象’生成矩阵’

a1=c(1:12)
matrix(a1,nrow=3,ncol=4)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
matrix(a1,nrow=3,ncol=3,byrow=T)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
matrix(a1,nrow=4,ncol=3,byrow=T)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
matrix(a1,nrow=4,ncol=-3)
错误于matrix(a1, nrow = 4, ncol = -3) : ‘ncol’值不对(< 0)
matrix(a1,nrow=4,ncol=3)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12

a=matrix(1:12,nrow=3,ncol=4)
a
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
a=b=matrix(1:12,nrow=3,ncol=4)
a+b
[,1] [,2] [,3] [,4]
[1,] 2 8 14 20
[2,] 4 10 16 22
[3,] 6 12 18 24
t[a]
错误于t[a] : 类别为’closure’的对象不可以取子集
t[a]
错误于t[a] : 类别为’closure’的对象不可以取子集

t(a)\
错误: 意外的输入 in “t(a)\”
t(a)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12

a-b
[,1] [,2] [,3] [,4]
[1,] 0 0 0 0
[2,] 0 0 0 0
[3,] 0 0 0 0

矩阵相乘
错误: 找不到对象’矩阵相乘’
矩阵相乘,函数diag() ,求对角线
a=matrix(1:12,nrow=3,ncol=4)
b=matrix(1:12,nrow=4,ncol=3)
a%*%b
[,1] [,2] [,3]
[1,] 70 158 246
[2,] 80 184 288
[3,] 90 210 330
a=matrix(1:16,nrow=4,ncol=4)
a
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
diag(a)
[1] 1 6 11 16
diag(diag(a))
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 6 0 0
[3,] 0 0 11 0
[4,] 0 0 0 16
diag(4)
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 0 0 1 0
[4,] 0 0 0 1
矩阵求逆
函数rnorm(),solve()

a=matrix(rnorm(16),4,4)
a
[,1] [,2] [,3] [,4]
[1,] -0.7829674 -0.6780748 0.9979333 -1.2448243
[2,] 0.2199471 -0.2561652 1.3312290 0.2504958
[3,] -0.3801423 0.3456489 -1.1471610 0.1030870
[4,] 0.8911887 -0.2393774 -1.4396885 0.7687524
解方程组
b=c(1:4)
b
[1] 1 2 3 4
solve(a,b)
[1] -12.014847 -12.772931 -1.347213 12.631347

矩阵的特征值与特征向量
错误: 找不到对象’矩阵的特征值与特征向量’
函数eigen()\
错误: 意外的输入 in “函数eigen()\”
a=diag(4)+1
a
[,1] [,2] [,3] [,4]
[1,] 2 1 1 1
[2,] 1 2 1 1
[3,] 1 1 2 1
[4,] 1 1 1 2
a.e=eigen(a,symmetric=T)
a.e
错误: 意外的输入 in “a.e\”
a.e
$values
[1] 5 1 1 1

$vectors
[,1] [,2] [,3] [,4]
[1,] -0.5 0.8660254 0.0000000 0.0000000
[2,] -0.5 -0.2886751 -0.5773503 -0.5773503
[3,] -0.5 -0.2886751 -0.2113249 0.7886751
[4,] -0.5 -0.2886751 0.7886751 -0.2113249

..7

矩阵的特征值与特征向量
错误: 找不到对象’矩阵的特征值与特征向量’
函数eigen()\
错误: 意外的输入 in “函数eigen()\”
a=diag(4)+1
a
[,1] [,2] [,3] [,4]
[1,] 2 1 1 1
[2,] 1 2 1 1
[3,] 1 1 2 1
[4,] 1 1 1 2
a.e=eigen(a,symmetric=T)
a.e\
错误: 意外的输入 in “a.e\”
a.e
$values
[1] 5 1 1 1

$vectors
[,1] [,2] [,3] [,4]
[1,] -0.5 0.8660254 0.0000000 0.0000000
[2,] -0.5 -0.2886751 -0.5773503 -0.5773503
[3,] -0.5 -0.2886751 -0.2113249 0.7886751
[4,] -0.5 -0.2886751 0.7886751 -0.2113249

eigen(M)
错误于as.matrix(x) : 找不到对象’M’
eigen(a)
$values
[1] 5 1 1 1

$vectors
[,1] [,2] [,3] [,4]
[1,] -0.5 0.8660254 0.0000000 0.0000000
[2,] -0.5 -0.2886751 -0.5773503 -0.5773503
[3,] -0.5 -0.2886751 -0.2113249 0.7886751
[4,] -0.5 -0.2886751 0.7886751 -0.2113249

eigen(a) val[1]5111eigen(a) v a l [ 1 ] 5 1 1 1 e i g e n ( a ) vec
[,1] [,2] [,3] [,4]
[1,] -0.5 0.8660254 0.0000000 0.0000000
[2,] -0.5 -0.2886751 -0.5773503 -0.5773503
[3,] -0.5 -0.2886751 -0.2113249 0.7886751
[4,] -0.5 -0.2886751 0.7886751 -0.2113249
a.e vectors[,1][,2][,3][,4][1,]0.50.86602540.00000000.0000000[2,]0.50.28867510.57735030.5773503[3,]0.50.28867510.21132490.7886751[4,]0.50.28867510.78867510.2113249t(a.e v e c t o r s [ , 1 ] [ , 2 ] [ , 3 ] [ , 4 ] [ 1 , ] − 0.5 0.8660254 0.0000000 0.0000000 [ 2 , ] − 0.5 − 0.2886751 − 0.5773503 − 0.5773503 [ 3 , ] − 0.5 − 0.2886751 − 0.2113249 0.7886751 [ 4 , ] − 0.5 − 0.2886751 0.7886751 − 0.2113249 t ( a . e vectors)
[,1] [,2] [,3] [,4]
[1,] -0.5000000 -0.5000000 -0.5000000 -0.5000000
[2,] 0.8660254 -0.2886751 -0.2886751 -0.2886751
[3,] 0.0000000 -0.5773503 -0.2113249 0.7886751
[4,] 0.0000000 -0.5773503 0.7886751 -0.2113249
a.e vectors v e c t o r s values)
[,1] [,2] [,3] [,4]
[1,] -2.5 0.8660254 0.0000000 0.0000000
[2,] -2.5 -0.2886751 -0.5773503 -0.5773503
[3,] -2.5 -0.2886751 -0.2113249 0.7886751
[4,] -2.5 -0.2886751 0.7886751 -0.2113249…..
eigen(a)
$values
[1] 5 1 1 1

$vectors
[,1] [,2] [,3] [,4]
[1,] -0.5 0.8660254 0.0000000 0.0000000
[2,] -0.5 -0.2886751 -0.5773503 -0.5773503
[3,] -0.5 -0.2886751 -0.2113249 0.7886751
[4,] -0.5 -0.2886751 0.7886751 -0.2113249

eigen(a) val[1]5111eigen(a) v a l [ 1 ] 5 1 1 1 e i g e n ( a ) vec
[,1] [,2] [,3] [,4]
[1,] -0.5 0.8660254 0.0000000 0.0000000
[2,] -0.5 -0.2886751 -0.5773503 -0.5773503
[3,] -0.5 -0.2886751 -0.2113249 0.7886751
[4,] -0.5 -0.2886751 0.7886751 -0.2113249

a.e vectors[,1][,2][,3][,4][1,]0.50.86602540.00000000.0000000[2,]0.50.28867510.57735030.5773503[3,]0.50.28867510.21132490.7886751[4,]0.50.28867510.78867510.2113249a.e v e c t o r s [ , 1 ] [ , 2 ] [ , 3 ] [ , 4 ] [ 1 , ] − 0.5 0.8660254 0.0000000 0.0000000 [ 2 , ] − 0.5 − 0.2886751 − 0.5773503 − 0.5773503 [ 3 , ] − 0.5 − 0.2886751 − 0.2113249 0.7886751 [ 4 , ] − 0.5 − 0.2886751 0.7886751 − 0.2113249 a . e vectors%*%diag(a.e values)[,1][,2][,3][,4][1,]2.50.86602540.00000000.0000000[2,]2.50.28867510.57735030.5773503[3,]2.50.28867510.21132490.7886751[4,]2.50.28867510.78867510.2113249a.e v a l u e s ) [ , 1 ] [ , 2 ] [ , 3 ] [ , 4 ] [ 1 , ] − 2.5 0.8660254 0.0000000 0.0000000 [ 2 , ] − 2.5 − 0.2886751 − 0.5773503 − 0.5773503 [ 3 , ] − 2.5 − 0.2886751 − 0.2113249 0.7886751 [ 4 , ] − 2.5 − 0.2886751 0.7886751 − 0.2113249 a . e vectors%*%diag(a.e values) v a l u e s ) vectors)

x=c(1:6)
x
[1] 1 2 3 4 5 6
x
[1] 1 2 3 4 5 6
is.vector(x)
[1] TRUE

x=c(1:6)
x
[1] 1 2 3 4 5 6
x
[1] 1 2 3 4 5 6
is.vector(x)
[1] TRUE
is.array(x)
[1] FALSE
dim(a)<-c(2,3)
错误于dim(a) <- c(2, 3) : dims [product 6]与对象长度[16]不匹配
a=c(1:6)
dim(a)<-c(2,3)
a
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6

is.array(a)
[1] TRUE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值