【问题标题】:Partition a matrix into list of sub matrices row-wise in R在R中将矩阵按行划分为子矩阵列表
【发布时间】:2016-08-09 04:02:54
【问题描述】:

我有一个矩阵,其中每一行都是 1 或 0 到 1 之间的任何数字。我需要将其划分为矩阵列表,以便

  1. 每个子矩阵要么全为 1,要么全为 0 到 1 之间的数字
  2. 使用 rbind 组合列表的所有子矩阵产生原始矩阵

例子:

m <- 
structure(c(1, 1, 1, 0.84, 0.27, 0.24, 0.48, 0.28, 0.62, 0.55, 
1, 1, 0.26, 0.93, 0.87, 0.76, 1, 1, 1, 1, 1, 0.31, 0.32, 0.96, 
0.25, 0.96, 0.43, 0.66, 1, 1, 0.22, 0.88, 0.01, 0.14, 1, 1, 1, 
1, 1, 0.71, 0.99, 0.6, 0.22, 0.73, 0.54, 0.17, 1, 1, 0.59, 0.67, 
0.07, 0.4, 1, 1, 1, 1, 1, 0.27, 0.62, 0.52, 0.67, 0.69, 0.06, 
0.63, 1, 1, 0.27, 0.95, 0.16, 0.22, 1, 1, 1, 1, 1, 0.59, 0.94, 
0.4, 0.05, 0.05, 0.26, 0.31, 1, 1, 0.53, 0.52, 0.77, 0.06, 1, 
1, 1, 1, 1, 0.48, 0.47, 0.88, 0.7, 0.4, 0.4, 0.72, 1, 1, 0.79, 
0.58, 0.74, 0.4, 1, 1, 1, 1, 1, 0.27, 0.41, 0.36, 0.35, 0.48, 
0.2, 0.4, 1, 1, 0.17, 0.34, 0.97, 0.06, 1, 1, 1, 1, 1, 0.56, 
0.66, 0.29, 0.41, 0.56, 0.83, 0.97, 1, 1, 0.4, 0.35, 0.47, 0.23, 
1, 1, 1, 1, 1, 0.91, 0.15, 0.17, 0.82, 0.7, 0.15, 0.97, 1, 1, 
0.47, 0.02, 0.07, 0.05, 1, 1, 1, 1, 1, 0.9, 0.57, 0.17, 0.92, 
0.92, 0.8, 0.73, 1, 1, 0.87, 0.5, 0.65, 0.67, 1, 1), .Dim = c(18L, 
10L))

L <- 
list(structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), .Dim = c(3L, 10L
)), structure(c(0.84, 0.27, 0.24, 0.48, 0.28, 0.62, 0.55, 0.31, 
0.32, 0.96, 0.25, 0.96, 0.43, 0.66, 0.71, 0.99, 0.6, 0.22, 0.73, 
0.54, 0.17, 0.27, 0.62, 0.52, 0.67, 0.69, 0.06, 0.63, 0.59, 0.94, 
0.4, 0.05, 0.05, 0.26, 0.31, 0.48, 0.47, 0.88, 0.7, 0.4, 0.4, 
0.72, 0.27, 0.41, 0.36, 0.35, 0.48, 0.2, 0.4, 0.56, 0.66, 0.29, 
0.41, 0.56, 0.83, 0.97, 0.91, 0.15, 0.17, 0.82, 0.7, 0.15, 0.97, 
0.9, 0.57, 0.17, 0.92, 0.92, 0.8, 0.73), .Dim = c(7L, 10L)), 
    structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1), .Dim = c(2L, 10L)), structure(c(0.26, 0.93, 
    0.87, 0.76, 0.22, 0.88, 0.01, 0.14, 0.59, 0.67, 0.07, 0.4, 
    0.27, 0.95, 0.16, 0.22, 0.53, 0.52, 0.77, 0.06, 0.79, 0.58, 
    0.74, 0.4, 0.17, 0.34, 0.97, 0.06, 0.4, 0.35, 0.47, 0.23, 
    0.47, 0.02, 0.07, 0.05, 0.87, 0.5, 0.65, 0.67), .Dim = c(4L, 
    10L)), structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1), .Dim = c(2L, 10L)))

【问题讨论】:

  • 请不要发布数据图片。使您的示例可重现。而且,它必须是矩阵吗?
  • 如果 m 是您的输入矩阵,L 是您的输出列表,则将 R 中运行 dput(m)dput(L) 的输出粘贴到您的问题中。

标签: r matrix split partition


【解决方案1】:

这里有一个解决方案。为方便起见,它使用 data.table 的 rleid。

首先,我们生成一些数据:

set.seed(123)
input <- matrix(runif(180),ncol=10)
input[c(1:3,5,9:10),]<-1

然后我们确定哪些行只有一个

is_one <- apply(input,1,function(x){all(x==1)})

我们想按组分割我们的行是一/不是一,所以我们生成行 id 和分割 id

row_ids <- 1:nrow(input)
split_ids <- data.table::rleid(is_one)

我们生成输出,方法是将行 ID 按组拆分为一/非一,并返回每组行 ID 的输入数据。

output <- lapply(split(row_ids,split_ids),function(x){input[x,]})

【讨论】:

  • split.data.frame(input, split_ids)
  • @Srikrishna ;如果这回答了您的问题,您可以根据需要单击勾选标记将其标记为如此(如果您愿意,也可以通过单击向上箭头-stackoverflow.com/help/someone-answers 来投赞成票)。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多