融化R中的下半矩阵

发布于 2024-12-17 08:55:39 字数 804 浏览 0 评论 0原文

如何融化下半三角形加对角矩阵?

11 NA NA  NA  NA
12 22 NA  NA  NA
13 23 33  NA  NA
14 24 34  44  NA
15 25 35  45  55
    A <- t(matrix (c(11,  NA, NA,  NA,  NA, 12, 22, NA,  NA,  NA,
   13, 23, 33,  NA,  NA, 14, 24, 34,  44,  NA,15, 25, 
   35,  45,  55), ncol = 5)) 

 > A
     [,1] [,2] [,3] [,4] [,5]
[1,]   11   NA   NA   NA   NA
[2,]   12   22   NA   NA   NA
[3,]   13   23   33   NA   NA
[4,]   14   24   34   44   NA
[5,]   15   25   35   45   55 

到 row 和 col 中的 data.frame (保留以下顺序)

col  row   value 
1     1      11
1     2      12
1     3      13
1     4      14
1     5      15
2     2      22
2     3      23
2     4      24
2     5      25
3     3      33
3     4      34
3     5      35
4     4      44
4     5      45
5     5      55

How can I melt a lower half triangle plus diagonal matrix ?

11 NA NA  NA  NA
12 22 NA  NA  NA
13 23 33  NA  NA
14 24 34  44  NA
15 25 35  45  55
    A <- t(matrix (c(11,  NA, NA,  NA,  NA, 12, 22, NA,  NA,  NA,
   13, 23, 33,  NA,  NA, 14, 24, 34,  44,  NA,15, 25, 
   35,  45,  55), ncol = 5)) 

 > A
     [,1] [,2] [,3] [,4] [,5]
[1,]   11   NA   NA   NA   NA
[2,]   12   22   NA   NA   NA
[3,]   13   23   33   NA   NA
[4,]   14   24   34   44   NA
[5,]   15   25   35   45   55 

To data.frame in row and col (preserving the following order)

col  row   value 
1     1      11
1     2      12
1     3      13
1     4      14
1     5      15
2     2      22
2     3      23
2     4      24
2     5      25
3     3      33
3     4      34
3     5      35
4     4      44
4     5      45
5     5      55

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寂寞花火° 2024-12-24 08:55:39

如果您也希望索引作为列,这应该可行:

m <- matrix(1:25,5,5)
m[upper.tri(m)] <- NA
m

     [,1] [,2] [,3] [,4] [,5]
[1,]    1   NA   NA   NA   NA
[2,]    2    7   NA   NA   NA
[3,]    3    8   13   NA   NA
[4,]    4    9   14   19   NA
[5,]    5   10   15   20   25

cbind(which(!is.na(m),arr.ind = TRUE),na.omit(as.vector(m)))
      row col   
 [1,]   1   1  1
 [2,]   2   1  2
 [3,]   3   1  3
 [4,]   4   1  4
 [5,]   5   1  5
 [6,]   2   2  7
 [7,]   3   2  8
 [8,]   4   2  9
 [9,]   5   2 10
[10,]   3   3 13
[11,]   4   3 14
[12,]   5   3 15
[13,]   4   4 19
[14,]   5   4 20
[15,]   5   5 25

我想我会解释一下。我使用了三个“技巧”:

  1. whicharr.ind 参数来获取索引
  2. 需要避免的非常有用的 na.omit 函数一些额外的输入
  3. R 以列主形式存储矩阵,因此 as.vector 以正确的顺序返回值。

If you want the indices as columns as well, this should work:

m <- matrix(1:25,5,5)
m[upper.tri(m)] <- NA
m

     [,1] [,2] [,3] [,4] [,5]
[1,]    1   NA   NA   NA   NA
[2,]    2    7   NA   NA   NA
[3,]    3    8   13   NA   NA
[4,]    4    9   14   19   NA
[5,]    5   10   15   20   25

cbind(which(!is.na(m),arr.ind = TRUE),na.omit(as.vector(m)))
      row col   
 [1,]   1   1  1
 [2,]   2   1  2
 [3,]   3   1  3
 [4,]   4   1  4
 [5,]   5   1  5
 [6,]   2   2  7
 [7,]   3   2  8
 [8,]   4   2  9
 [9,]   5   2 10
[10,]   3   3 13
[11,]   4   3 14
[12,]   5   3 15
[13,]   4   4 19
[14,]   5   4 20
[15,]   5   5 25

I guess I'll explain this a bit. I'm using three "tricks":

  1. The arr.ind argument to which to get the indices
  2. The very useful na.omit function to avoid some extra typing
  3. The fact that R stores matrices in column major form, hence as.vector returns the values in the right order.
北方的韩爷 2024-12-24 08:55:39

我的一艘班轮。

reshape2::melt(A, varnames = c('row', 'col'), na.rm = TRUE)

My one liner.

reshape2::melt(A, varnames = c('row', 'col'), na.rm = TRUE)
花海 2024-12-24 08:55:39

这是我的第一个解决方案:

test <- rbind(c(11,NA,NA,NA,NA),
  c(12,22,NA,NA,NA),
  c(13,23,33,NA,NA),
  c(14,24,34,44,NA),
  c(15,25,35,45,55))  ## Load the matrix

test2 <- as.vector(test)  ## "melt" it into a vector

test <- cbind( test2[!is.na(test2)] )  ## get rid of NAs, cbind it into a column 

结果是:

> test
      [,1]
 [1,]   11
 [2,]   12
 [3,]   13
 [4,]   14
 [5,]   15
 [6,]   22
 [7,]   23
 [8,]   24
 [9,]   25
[10,]   33
[11,]   34
[12,]   35
[13,]   44
[14,]   45
[15,]   55

或者,您可以使用矩阵命令:

test <- rbind(c(11,NA,NA,NA,NA),
  c(12,22,NA,NA,NA),
  c(13,23,33,NA,NA),
  c(14,24,34,44,NA),
  c(15,25,35,45,55))  ## Load the matrix

test2 <- matrix(test, ncol=1)  
test <- cbind( test2[!is.na(test2), ] )   
  ## same as above, except now explicitly noting rows to replace.

Here's my first solution:

test <- rbind(c(11,NA,NA,NA,NA),
  c(12,22,NA,NA,NA),
  c(13,23,33,NA,NA),
  c(14,24,34,44,NA),
  c(15,25,35,45,55))  ## Load the matrix

test2 <- as.vector(test)  ## "melt" it into a vector

test <- cbind( test2[!is.na(test2)] )  ## get rid of NAs, cbind it into a column 

Results are:

> test
      [,1]
 [1,]   11
 [2,]   12
 [3,]   13
 [4,]   14
 [5,]   15
 [6,]   22
 [7,]   23
 [8,]   24
 [9,]   25
[10,]   33
[11,]   34
[12,]   35
[13,]   44
[14,]   45
[15,]   55

Alternatively, you can use the matrix command:

test <- rbind(c(11,NA,NA,NA,NA),
  c(12,22,NA,NA,NA),
  c(13,23,33,NA,NA),
  c(14,24,34,44,NA),
  c(15,25,35,45,55))  ## Load the matrix

test2 <- matrix(test, ncol=1)  
test <- cbind( test2[!is.na(test2), ] )   
  ## same as above, except now explicitly noting rows to replace.
音栖息无 2024-12-24 08:55:39

这是我的尝试:

# enter the data
df <- c(11,12,13,14,15,NA,22,23,24,25,NA,NA,33,34,35,NA,NA,NA,44,45,NA,NA,NA,NA,55)
dim(df) <- c(5,5)
df

# make new data frame with rows and column indicators
melteddf <- data.frame(
value=df[lower.tri(df,diag=T)],
col=rep(1:ncol(df),ncol(df):1),
row=unlist(sapply(1:nrow(df),function(x) x:nrow(df)))
)

我希望我之前就知道 cbind 的 arr.ind 部分。

Here is my attempt:

# enter the data
df <- c(11,12,13,14,15,NA,22,23,24,25,NA,NA,33,34,35,NA,NA,NA,44,45,NA,NA,NA,NA,55)
dim(df) <- c(5,5)
df

# make new data frame with rows and column indicators
melteddf <- data.frame(
value=df[lower.tri(df,diag=T)],
col=rep(1:ncol(df),ncol(df):1),
row=unlist(sapply(1:nrow(df),function(x) x:nrow(df)))
)

I wish I knew about the arr.ind part of cbind which before now though.

爱你不解释 2024-12-24 08:55:39

这是使用 arrayInd 的方法,它与 @joran 的方法基本相同,但在其他设置中可能有用:

na.omit( data.frame(arrayInd(1:prod(dim(A)), dim(A)), value=c(A)) )
   X1 X2 value
1   1  1    11
2   2  1    12
3   3  1    13
4   4  1    14
5   5  1    15
7   2  2    22
8   3  2    23
9   4  2    24
10  5  2    25
13  3  3    33
14  4  3    34
15  5  3    35
19  4  4    44
20  5  4    45
25  5  5    55

Here is a method using arrayInd which is basically the same as @joran's but might be useful in other settings:

na.omit( data.frame(arrayInd(1:prod(dim(A)), dim(A)), value=c(A)) )
   X1 X2 value
1   1  1    11
2   2  1    12
3   3  1    13
4   4  1    14
5   5  1    15
7   2  2    22
8   3  2    23
9   4  2    24
10  5  2    25
13  3  3    33
14  4  3    34
15  5  3    35
19  4  4    44
20  5  4    45
25  5  5    55
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文