从另一个矩阵中将子集的ID作为子集 [英] subsetting matrix with id from another matrix

查看:60
本文介绍了从另一个矩阵中将子集的ID作为子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用第二个矩阵中的数据对一个矩阵的数据进行子集化.一个矩阵的列被标记.例如,

area1 <- c(9836374,635440,23018,833696,936079,1472449,879042,220539,870581,217418,552303,269359,833696,936079,1472449,879042,220539,870581, 833696,936079,1472449,879042,220539,870581)
id <- c(1,2,5,30,31,34,1,2,5,1,2,5,1,2,5,30,31,34,51,52,55,81,82,85)
mat1 <- matrix(area1, ncol=3, byrow=T)
mat2 <- matrix(id, ncol=3, byrow=T)
dimnames(mat1) <-list(NULL, c("a1","a2","a3"))   

mat2包含mat1的id,所以矩阵的尺寸是相同的(即mat1[1,1]标识mat2[1,1].我想要的是在具有值的行时创建mat1的子矩阵) c(1, 2, 5)出现在mat2中.在本微型示例中,子矩阵1将具有2行数据,子矩阵2和3将分别具有1行,而子矩阵4将具有来自mat1的4行数据.后面各行之间有1,3,5的行数变化.这有意义吗?

最初,矩阵是从数据帧转换而成的,其中id在第一列中,面积在第二列中.我找不到在数据框内的1行之间分配变量行的子集的方法,这就是为什么我切换到矩阵的原因.

解决方案

我认为这已涵盖并符合您的描述:

spl <- cumsum(apply(mat2,1, function(x) all(x==c(1,2,5))))
split(as.data.frame(mat1),spl)

#$`1`
#       a1     a2      a3
#1 9836374 635440   23018
#2  833696 936079 1472449
# 
#$`2`
#      a1     a2     a3
#3 879042 220539 870581
#
#$`3`
#      a1     a2     a3
#4 217418 552303 269359
#
#$`4`
#      a1     a2      a3
#5 833696 936079 1472449
#6 879042 220539  870581
#7 833696 936079 1472449
#8 879042 220539  870581

结果符合"子矩阵1将有2行数据,子矩阵2和3分别具有1行,而子矩阵4将具有来自mat1的4行数据"

I would like to subset the data of one matrix using data in a second matrix. The columns of one matrix is labeled. For example,

area1 <- c(9836374,635440,23018,833696,936079,1472449,879042,220539,870581,217418,552303,269359,833696,936079,1472449,879042,220539,870581, 833696,936079,1472449,879042,220539,870581)
id <- c(1,2,5,30,31,34,1,2,5,1,2,5,1,2,5,30,31,34,51,52,55,81,82,85)
mat1 <- matrix(area1, ncol=3, byrow=T)
mat2 <- matrix(id, ncol=3, byrow=T)
dimnames(mat1) <-list(NULL, c("a1","a2","a3"))   

mat2 contains the ids for mat1, so the dimensions of the matrix are the same (i.e., mat1[1,1] identifies mat2[1,1]. What I want is to create submatrices of mat1 when the row with values c(1, 2, 5) shows up in mat2. In this present mini example, submatrix 1 would have 2 rows of data, submatrix 2 and 3 have 1 row each, and submatrix 4 would have 4 rows of data from mat1. The number of rows between subsequent rows with 1,3,5 varies. Does this make sense?

Originally, the matrices were transformed from a dataframe, with id in one column and area in a second column. I couldn't find a way to subset variable rows between rows of 1 within a dataframe, which is why I switched to a matrix.

解决方案

I think this covers it and fits with your description:

spl <- cumsum(apply(mat2,1, function(x) all(x==c(1,2,5))))
split(as.data.frame(mat1),spl)

#$`1`
#       a1     a2      a3
#1 9836374 635440   23018
#2  833696 936079 1472449
# 
#$`2`
#      a1     a2     a3
#3 879042 220539 870581
#
#$`3`
#      a1     a2     a3
#4 217418 552303 269359
#
#$`4`
#      a1     a2      a3
#5 833696 936079 1472449
#6 879042 220539  870581
#7 833696 936079 1472449
#8 879042 220539  870581

The result fits with "submatrix 1 would have 2 rows of data, submatrix 2 and 3 have 1 row each, and submatrix 4 would have 4 rows of data from mat1"

这篇关于从另一个矩阵中将子集的ID作为子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆