在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、创建测试矩阵 > rname <- paste0("r",1:4)
> rname
[1] "r1" "r2" "r3" "r4"
> cname <- paste0("c",1:5)
> cname
[1] "c1" "c2" "c3" "c4" "c5"
> a <- matrix(1:20, nrow = 4, ncol = 5, byrow = T,
+ dimnames = list(rname,cname))
> a
c1 c2 c3 c4 c5
r1 1 2 3 4 5
r2 6 7 8 9 10
r3 11 12 13 14 15
r4 16 17 18 19 20
2、列行输出 > c1 <- rep(1:nrow(a), ncol(a))
> c1
[1] 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
> c2 <- rep(1:ncol(a),each = nrow(a))
> c2
[1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5
> out1 <- data.frame(c1,c2,c3 = a[1:length(a)])
> out1
c1 c2 c3
1 1 1 1
2 2 1 6
3 3 1 11
4 4 1 16
5 1 2 2
6 2 2 7
7 3 2 12
8 4 2 17
9 1 3 3
10 2 3 8
11 3 3 13
12 4 3 18
13 1 4 4
14 2 4 9
15 3 4 14
16 4 4 19
17 1 5 5
18 2 5 10
19 3 5 15
20 4 5 20
3、行列输出 > c1 <- rep(1:nrow(a),each = ncol(a))
> c1
[1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
> c2 <- rep(1:ncol(a),nrow(a))
> c2
[1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
> b <- matrix(1:length(a), nrow = ncol(a), ncol = nrow(a),
+ byrow = T)
> b
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
[5,] 17 18 19 20
> out2 <- data.frame(c1, c2, c3 = a[b] )
> out2
c1 c2 c3
1 1 1 1
2 1 2 2
3 1 3 3
4 1 4 4
5 1 5 5
6 2 1 6
7 2 2 7
8 2 3 8
9 2 4 9
10 2 5 10
11 3 1 11
12 3 2 12
13 3 3 13
14 3 4 14
15 3 5 15
16 4 1 16
17 4 2 17
18 4 3 18
19 4 4 19
20 4 5 20
|
请发表评论