向量取值[]将TRUE对应的值取出
> x=c(7,5,3,6,1)
> x>3
[1] TRUE TRUE FALSE TRUE FALSE
> x[x>3]
[1] 7 5 6
>
> x=sample(1:100,28)
> x
[1] 53 43 47 41 38 35 20 75 99 4 95 82 7 78 66 65 31
[18] 56 51 84 12 14 39 86 57 91 9 87
> matrix(x,nrow=7)
[,1] [,2] [,3] [,4]
[1,] 53 75 66 14
[2,] 43 99 65 39
[3,] 47 4 31 86
[4,] 41 95 56 57
[5,] 38 82 51 91
[6,] 35 7 84 9
[7,] 20 78 12 87
> x1=sample(1:100,7);×7
```将向量进行二维排列,矩阵
x1=sample(1:100,7);x1
[1] 67 65 6 69 94 71 68
x2=11:17;x2
[1] 11 12 13 14 15 16 17
x3=sample(1:10,7);x3
[1] 9 4 2 10 8 3 5
cbind(x1,x2,x3)
x1 x2 x3
[1,] 67 11 9
[2,] 65 12 4
[3,] 6 13 2
[4,] 69 14 10
[5,] 94 15 8
[6,] 71 16 3
[7,] 68 17 5
将元素数量长度相同,数据类型相同的向量合在一个矩阵
将元素数量长度相同,数据类型不相同的向量合在一个数据框
y1=sample(1:100,7);y1
[1] 100 57 4 9 39 68 64
y2=letters[1:7];y2
[1] "a" "b" "c" "d" "e" "f" "g"
y3=sample(1:10,7);y3
[1] 3 8 5 6 4 7 9
data.frame(y1,y2,y3)
y1 y2 y3
1 100 a 3
2 57 b 8
3 4 c 5
4 9 d 6
5 39 e 4
6 68 f 7
7 64 g 9
列表:长度不同,数据类型不同,多级元素
网友评论