0

基本上,我有几个频率表 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"))
4

3 回答 3

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
于 2013-08-20T03:26:37.667 回答
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

如果像 thelatemail 认为的那样(尽管我不认为)这些是单行表,那就更容易了:

 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
于 2013-08-20T02:12:09.573 回答
0

如果你把你的d1andd2变成data.tables,你可以很容易地通过一个公共键来合并它们:

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为矩阵,将NAs替换为0s 等。

于 2013-08-20T02:05:06.507 回答