创建一个由向量​​名称索引的矩阵

时间:2013-08-20 01:43:26

标签: r matrix indexing

基本上,我有几个频率表d1和d2。假设我有:

UPDATE2:d1的实际结构是表。因此d1由d1 <- table(datavector)获得,类似于d2。

d1
  Value     0    1    2    3    4    9                         
  Freq     25   30  100   10   10   10

d2
  Value     0    1    3    5    7   11    13
   Freq    25   30  100   10   10   10    12

问题:我想生成一个矩阵,其行对应于d1和d2,列对应于d1和d2中所见的所有不同“值”。所以我想生成一个行和列的矩阵,如下所示:

     [,"0"] [,"1"] [,"2"] [,"3"] [,"4"] [,"5"] [,"7"] [,"9"] [,"11"] [,"13"]
[1,]    25     30    100    10     10    0       0      10     0         0
[2,]    25     30     0     100     0    10      10     0      10        12

请注意,没有列号6,8和10,因为它们没有出现在频率表中。最后,我试图将此矩阵放入函数image.plot()

更新1:我想我可以允许列号6,8和10出现在矩阵中,但最终我必须编写一个for循环来消除仅包含零个列的列。

更新3:请注意,我实际上使用250个数据向量,因此有250个表(每个表具有不同的长度/维度)。所以,我正在寻找一个有效的解决方案

更新4:请将上述内容视为我想要实现的内容的摘要。真实数据集如下:

> dput(head(get.dist(fnn[1])))
structure(c(0.999214894571557, 0.000134589502018843, 4.48631673396142e-05, 
2.24315836698071e-05, 6.72947510094213e-05, 8.97263346792284e-05, 
2.24315836698071e-05, 4.48631673396142e-05, 4.48631673396142e-05, 
2.24315836698071e-05, 2.24315836698071e-05, 6.72947510094213e-05, 
2.24315836698071e-05, 2.24315836698071e-05, 4.48631673396142e-05, 
2.24315836698071e-05, 6.72947510094213e-05, 2.24315836698071e-05
), class = "table", .Dim = 18L, .Dimnames = structure(list(d = c("0", 
"1", "2", "3", "4", "5", "8", "9", "11", "12", "15", "16", "17", 
"18", "20", "22", "24", "31")), .Names = "d"))

> dput(head(get.dist(fnn[2])))
structure(c(0.71161956034096, 0.199147599820547, 0.0644010767160162, 
0.0147599820547331, 0.00327501121579183, 0.000807537012113055, 
6.72947510094213e-05, 0.000785105428443248, 0.000179452669358457, 
0.000134589502018843, 0.000112157918349035, 4.48631673396142e-05, 
6.72947510094213e-05, 0.00307312696276357, 0.00107671601615074, 
0.000336473755047106, 6.72947510094213e-05, 2.24315836698071e-05, 
2.24315836698071e-05), class = "table", .Dim = 19L, .Dimnames = structure(list(
    d = c("0", "1", "2", "3", "4", "5", "6", "9", "10", "11", 
    "35", "36", "37", "38", "39", "40", "41", "42", "43")), .Names = "d"))

> dput(head(get.dist(fnn[3])))
structure(c(0.747353073126963, 0.13138178555406, 0.0295423956931359, 
0.0139075818752804, 0.0119560340960072, 0.0151861821444594, 0.0243382682817407, 
0.00697622252131, 0.00255720053835801, 0.00161507402422611, 0.00293853746074473, 
0.00116644235082997, 0.004419021982952, 0.0018842530282638, 0.000628084342754598, 
0.00053835800807537, 0.000448631673396142, 0.000493494840735756, 
0.000650515926424406, 0.000403768506056528, 0.000269179004037685, 
0.000179452669358457, 0.000269179004037685, 0.000179452669358457, 
8.97263346792284e-05, 0.000246747420367878, 4.48631673396142e-05, 
4.48631673396142e-05, 4.48631673396142e-05, 2.24315836698071e-05, 
2.24315836698071e-05, 4.48631673396142e-05, 2.24315836698071e-05, 
2.24315836698071e-05, 2.24315836698071e-05, 2.24315836698071e-05, 
2.24315836698071e-05, 2.24315836698071e-05, 2.24315836698071e-05
), class = "table", .Dim = 39L, .Dimnames = structure(list(d = c("0", 
"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", "27", "28", "30", "32", "33", "34", "36", "37", 
"38", "43", "54", "67")), .Names = "d"))

> dput(head(get.dist(fnn[4])))
structure(c(0.217743382682817, 0.49416778824585, 0.135150291610588, 
0.0331987438313145, 0.0243831314490803, 0.0431135038133692, 0.022790489008524, 
0.00912965455361149, 0.00614625392552714, 0.00937640197397936, 
0.00244504262000897, 0.000560789591745177, 0.000493494840735756, 
0.000448631673396142, 0.000336473755047106, 0.000112157918349035, 
0.000201884253028264, 4.48631673396142e-05, 4.48631673396142e-05, 
2.24315836698071e-05, 2.24315836698071e-05, 4.48631673396142e-05, 
2.24315836698071e-05), class = "table", .Dim = 23L, .Dimnames = structure(list(
    d = c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", 
    "11", "12", "13", "14", "15", "16", "17", "18", "19", "23", 
    "25", "45")), .Names = "d"))

3 个答案:

答案 0 :(得分:3)

这是一个使用Reduce的选项,似乎在提供数据时起作用:

# make a list including your 3 dput parts
keylist <- list(d1,d2,d3)
result <- Reduce(function(...) merge(..., by="d", all=T), keylist)
result <- transform(result,row.names=d,d=NULL)
result <- t(result)
rownames(result) <- NULL

似乎有效:

> result[,c(1:2,44:45)]
             0            1           54           67
[1,] 0.9992149 0.0001345895           NA           NA
[2,] 0.7116196 0.1991475998           NA           NA
[3,] 0.7473531 0.1313817856 2.243158e-05 2.243158e-05

答案 1 :(得分:1)

我使用的是数据帧,但如果d1和d2是矩阵,那么如果您删除了unlist次调用,这仍然有用:

 M <- matrix(0, nrow=2, ncol=12 )
 colnames(M) <- as.character(0:11)
 M[1 , as.character(d1[1 , 2:7]) ] <- unlist(d1[2, 2:7 ])
 M

#      0  1   2  3  4 5 6 7 8  9 10 11
#[1,] 25 30 100 10 10 0 0 0 0 10  0  0
#[2,]  0  0   0  0  0 0 0 0 0  0  0  0

 M[2 , as.character(d2[1 , 2:7]) ] <- unlist(d2[2, 2:7 ])
 M
#-------------------
      0  1   2   3  4  5 6  7 8  9 10 11
[1,] 25 30 100  10 10  0 0  0 0 10  0  0
[2,] 25 30   0 100  0 10 0 10 0  0  0 10

将我的示例转换为矩阵(从矩阵类继承索引):

 d1a <-data.matrix(d1[,-1])
 rownames(d1a) <- d1[,1]
 d2a <-data.matrix(d2[,-1])
 rownames(d2a) <- d2[,1]
 M[1 , as.character(d1a[1 , ]) ] <-d1a[2,  ]
 M[2 , as.character(d2a[1 , ]) ] <-d2a[2,  ]
 M
#---------
      0  1   2   3  4  5 6  7 8  9 10 11
[1,] 25 30 100  10 10  0 0  0 0 10  0  0
[2,] 25 30   0 100  0 10 0 10 0  0  0 10

如果aslatemail认为(虽然我没有)这些是一行表,那么它就更容易了:

 M[2 , colnames(d2b) ] <-d2b
 M[2 , colnames(d1b) ] <-d1b
 M

      0  1   2   3  4  5 6  7 8  9 10 11
[1,] 25 30 100  10 10  0 0  0 0 10  0  0
[2,] 25 30   0 100  0 10 0 10 0  0  0 10

,不要在这些上使用for循环:

> M[ , !colSums(M==0)==2]
      0  1   2   3  4  5  7  9 11
[1,] 25 30 100  10 10  0  0 10  0
[2,] 25 30   0 100  0 10 10  0 10

如果您不创建任何零列,则无需删除任何零列:

你可以用这种方式创建dist.list:

dist.list= lapply(fnn, get.dist)
# 3 element example built from your example

dist.list<-{}
dist.list[[1]] <-
structure(c(0.999214894571557, 0.000134589502018843, 4.48631673396142e-05, 
2.24315836698071e-05, 6.72947510094213e-05, 8.97263346792284e-05, 
2.24315836698071e-05, 4.48631673396142e-05, 4.48631673396142e-05, 
2.24315836698071e-05, 2.24315836698071e-05, 6.72947510094213e-05, 
2.24315836698071e-05, 2.24315836698071e-05, 4.48631673396142e-05, 
2.24315836698071e-05, 6.72947510094213e-05, 2.24315836698071e-05
), class = "table", .Dim = 18L, .Dimnames = structure(list(d = c("0", 
"1", "2", "3", "4", "5", "8", "9", "11", "12", "15", "16", "17", 
"18", "20", "22", "24", "31")), .Names = "d"))

 dist.list[[2]] <-
structure(c(0.71161956034096, 0.199147599820547, 0.0644010767160162, 
0.0147599820547331, 0.00327501121579183, 0.000807537012113055, 
6.72947510094213e-05, 0.000785105428443248, 0.000179452669358457, 
0.000134589502018843, 0.000112157918349035, 4.48631673396142e-05, 
6.72947510094213e-05, 0.00307312696276357, 0.00107671601615074, 
0.000336473755047106, 6.72947510094213e-05, 2.24315836698071e-05, 
2.24315836698071e-05), class = "table", .Dim = 19L, .Dimnames = structure(list(
    d = c("0", "1", "2", "3", "4", "5", "6", "9", "10", "11", 
    "35", "36", "37", "38", "39", "40", "41", "42", "43")), .Names = "d"))

 dist.list[[3]] <-
structure(c(0.747353073126963, 0.13138178555406, 0.0295423956931359, 
0.0139075818752804, 0.0119560340960072, 0.0151861821444594, 0.0243382682817407, 
0.00697622252131, 0.00255720053835801, 0.00161507402422611, 0.00293853746074473, 
0.00116644235082997, 0.004419021982952, 0.0018842530282638, 0.000628084342754598, 
0.00053835800807537, 0.000448631673396142, 0.000493494840735756, 
0.000650515926424406, 0.000403768506056528, 0.000269179004037685, 
0.000179452669358457, 0.000269179004037685, 0.000179452669358457, 
8.97263346792284e-05, 0.000246747420367878, 4.48631673396142e-05, 
4.48631673396142e-05, 4.48631673396142e-05, 2.24315836698071e-05, 
2.24315836698071e-05, 4.48631673396142e-05, 2.24315836698071e-05, 
2.24315836698071e-05, 2.24315836698071e-05, 2.24315836698071e-05, 
2.24315836698071e-05, 2.24315836698071e-05, 2.24315836698071e-05
), class = "table", .Dim = 39L, .Dimnames = structure(list(d = c("0", 
"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", "27", "28", "30", "32", "33", "34", "36", "37", 
"38", "43", "54", "67")), .Names = "d"))

all.names <- lapply(dist.list, names)
uniq.names <- unique(unlist(all.names))
M <- matrix(0, nrow=length(dist.list), ncol=length(uniq.names) )
colnames(M) <- uniq.names
for (i in seq_along(dist.list) ) {
          M[i, all.names[[i]] ] <- dist.list[[i]] }
M

前20列

             0            1            2            3            4
[1,] 0.9992149 0.0001345895 4.486317e-05 2.243158e-05 6.729475e-05
[2,] 0.7116196 0.1991475998 6.440108e-02 1.475998e-02 3.275011e-03
[3,] 0.7473531 0.1313817856 2.954240e-02 1.390758e-02 1.195603e-02
                5            8            9           11           12
[1,] 8.972633e-05 2.243158e-05 4.486317e-05 4.486317e-05 2.243158e-05
[2,] 8.075370e-04 0.000000e+00 7.851054e-04 1.345895e-04 0.000000e+00
[3,] 1.518618e-02 2.557201e-03 1.615074e-03 1.166442e-03 4.419022e-03
               15           16           17           18           20
[1,] 2.243158e-05 6.729475e-05 2.243158e-05 2.243158e-05 4.486317e-05
[2,] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
[3,] 5.383580e-04 4.486317e-04 4.934948e-04 6.505159e-04 2.691790e-04
# remainder excluded

答案 2 :(得分:0)

如果您将d1d2转换为data.table,则可以通过公用密钥轻松合并它们:

library(data.table)

> d1 <- data.table(value = c(0, 1, 2, 3, 4, 9), freq = c(25, 30, 100, 10, 10, 10))
> d2 <- data.table(value = c(0, 1, 3, 5, 7, 11), freq = c(25, 30, 100, 10, 10, 10))
> setkey(d1, value)
> setkey(d2, value)
> merge(d1, d2, all = TRUE)
   value freq.x freq.y
1:     0     25     25
2:     1     30     30
3:     2    100     NA
4:     3     10    100
5:     4     10     NA
6:     5     NA     10
7:     7     NA     10
8:     9     10     NA
9:    11     NA     10

然后,您可以将生成的data.table转换为矩阵,将NA替换为0 s等。