做一个绝对频率的矩阵

时间:2015-05-29 17:40:16

标签: r matrix replication

我有两个矩阵: 矩阵1,一个带有:

的向量
matrix(c(0.16, 0.24, 0.16, 0.08, 0.09, 0.12, 0.06, 0.04, 0.04, 0.01))

实际上,我使用函数" FOR"创建它,使用频率为0.4,0.3,0.2,0.1的Punnett广场。

第二,我有矩阵2:

                [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]      [,9]      [,10]    
      [1,] "A 1 A 1" "B 1 B 1" "C 1 C 1" "D 1 D 1" "E 1 E 1" "F 1 F 1" "G 1 G 1" "H 1 H 1" "I 1 I 1" "J 1 J 1"
      [2,] "A 1 A 2" "B 1 B 2" "C 1 C 2" "D 1 D 2" "E 1 E 2" "F 1 F 2" "G 1 G 2" "H 1 H 2" "I 1 I 2" "J 1 J 2"
      [3,] "A 1 A 3" "B 1 B 3" "C 1 C 3" "D 1 D 3" "E 1 E 3" "F 1 F 3" "G 1 G 3" "H 1 H 3" "I 1 I 3" "J 1 J 3"
      [4,] "A 1 A 4" "B 1 B 4" "C 1 C 4" "D 1 D 4" "E 1 E 4" "F 1 F 4" "G 1 G 4" "H 1 H 4" "I 1 I 4" "J 1 J 4"
      [5,] "A 2 A 2" "B 2 B 2" "C 2 C 2" "D 2 D 2" "E 2 E 2" "F 2 F 2" "G 2 G 2" "H 2 H 2" "I 2 I 2" "J 2 J 2"
      [6,] "A 2 A 3" "B 2 B 3" "C 2 C 3" "D 2 D 3" "E 2 E 3" "F 2 F 3" "G 2 G 3" "H 2 H 3" "I 2 I 3" "J 2 J 3"
      [7,] "A 2 A 4" "B 2 B 4" "C 2 C 4" "D 2 D 4" "E 2 E 4" "F 2 F 4" "G 2 G 4" "H 2 H 4" "I 2 I 4" "J 2 J 4"
      [8,] "A 3 A 3" "B 3 B 3" "C 3 C 3" "D 3 D 3" "E 3 E 3" "F 3 F 3" "G 3 G 3" "H 3 H 3" "I 3 I 3" "J 3 J 3"
      [9,] "A 3 A 4" "B 3 B 4" "C 3 C 4" "D 3 D 4" "E 3 E 4" "F 3 F 4" "G 3 G 4" "H 3 H 4" "I 3 I 4" "J 3 J 4"
     [10,] "A 4 A 4" "B 4 B 4" "C 4 C 4" "D 4 D 4" "E 4 E 4" "F 4 F 4" "G 4 G 4" "H 4 H 4" "I 4 I 4" "J 4 J 4"

我想要一个新表(行= 100,列= 10),矩阵2的每列都有矩阵1的频率。所以我将有16次" A1A1",24次&# 34; A1A2"等等。其他列也一样。 但我想对任何频率(矩阵1)和矩阵2的任意数量的列线执行此操作。

1 个答案:

答案 0 :(得分:0)

尝试

 out <- matrix(rep(c(m2),m1[row(m2)]*100),100,10)
 dim(out)
 #[1] 100  10

数据

m1 <- matrix(c(0.16, 0.24, 0.16, 0.08, 0.09, 0.12, 0.06, 0.04, 0.04, 0.01))

m2 <-  structure(c("A 1 A 1", "A 1 A 2", "A 1 A 3", "A 1 A 4", "A 2 A 2", 
"A 2 A 3", "A 2 A 4", "A 3 A 3", "A 3 A 4", "A 4 A 4", "B 1 B 1", 
"B 1 B 2", "B 1 B 3", "B 1 B 4", "B 2 B 2", "B 2 B 3", "B 2 B 4", 
"B 3 B 3", "B 3 B 4", "B 4 B 4", "C 1 C 1", "C 1 C 2", "C 1 C 3", 
"C 1 C 4", "C 2 C 2", "C 2 C 3", "C 2 C 4", "C 3 C 3", "C 3 C 4", 
"C 4 C 4", "D 1 D 1", "D 1 D 2", "D 1 D 3", "D 1 D 4", "D 2 D 2", 
"D 2 D 3", "D 2 D 4", "D 3 D 3", "D 3 D 4", "D 4 D 4", "E 1 E 1", 
"E 1 E 2", "E 1 E 3", "E 1 E 4", "E 2 E 2", "E 2 E 3", "E 2 E 4", 
"E 3 E 3", "E 3 E 4", "E 4 E 4", "F 1 F 1", "F 1 F 2", "F 1 F 3", 
"F 1 F 4", "F 2 F 2", "F 2 F 3", "F 2 F 4", "F 3 F 3", "F 3 F 4", 
"F 4 F 4", "G 1 G 1", "G 1 G 2", "G 1 G 3", "G 1 G 4", "G 2 G 2", 
"G 2 G 3", "G 2 G 4", "G 3 G 3", "G 3 G 4", "G 4 G 4", "H 1 H 1", 
"H 1 H 2", "H 1 H 3", "H 1 H 4", "H 2 H 2", "H 2 H 3", "H 2 H 4", 
"H 3 H 3", "H 3 H 4", "H 4 H 4", "I 1 I 1", "I 1 I 2", "I 1 I 3", 
"I 1 I 4", "I 2 I 2", "I 2 I 3", "I 2 I 4", "I 3 I 3", "I 3 I 4", 
"I 4 I 4", "J 1 J 1", "J 1 J 2", "J 1 J 3", "J 1 J 4", "J 2 J 2", 
"J 2 J 3", "J 2 J 4", "J 3 J 3", "J 3 J 4", "J 4 J 4"), .Dim = c(10L, 
10L))