在R中以14为步长从矩阵中选择子集并将其保存在列表中

时间:2021-08-02 16:20:17

i have a matrix containing 14000 columns and 15 rows (matrixA)

我有一个包含14000列和15行(matrixA)的矩阵

i want to be able to select subsets of that matrix and put it a new matrix and then save this matrix in a new list (containing all the subsets)

我希望能够选择该矩阵的子集并将其置为新矩阵,然后将此矩阵保存在新列表中(包含所有子集)

so for example i want to select the first the data with the first 14 columns and then for the next matrix i want to select the next 14.

所以例如我想选择第一个带有前14列的数据,然后对于下一个矩阵,我想选择下一个14。

it should be something like this:

它应该是这样的:

matrixA[,1:14]
matrixA[,15:28]
matrixA[,29:42]

and so on...

等等...

in stead of typing this 1000 times i want to know if theres a function in R to select data in steps of 14?

而不是输入这1000次,我想知道是否在R中的一个函数以14步为目的选择数据?

2 个解决方案

#1


1  

Just try:

你试一试:

   lapply(seq(1,14000,by=14),function(x) myMatrix[,x:(x+13)]) 

#2


0  

Other options include:

其他选择包括:

 dim(matrixA) <- c(15,14,1000) #convert matrix to `array` and convert back to `list` using

 lapply(seq(dim(matrixA)[3]), function(i) matrixA[,,i])

or

要么

 library(plyr)
 alply(matrixA,3)

Or

要么

 lapply(split(1:14000, ((1:14000)-1)%/%14 +1), function(i) matrixA[,i])

data

set.seed(24)
matrixA <- matrix(sample(1:200, 15*14000, replace=TRUE), nrow=15)

#1


1  

Just try:

你试一试:

   lapply(seq(1,14000,by=14),function(x) myMatrix[,x:(x+13)]) 

#2


0  

Other options include:

其他选择包括:

 dim(matrixA) <- c(15,14,1000) #convert matrix to `array` and convert back to `list` using

 lapply(seq(dim(matrixA)[3]), function(i) matrixA[,,i])

or

要么

 library(plyr)
 alply(matrixA,3)

Or

要么

 lapply(split(1:14000, ((1:14000)-1)%/%14 +1), function(i) matrixA[,i])

data

set.seed(24)
matrixA <- matrix(sample(1:200, 15*14000, replace=TRUE), nrow=15)