-6

How can I split a matrix into many submatrixes? Every submatrix should contain a selection of rows of the initial matrix.

The initial matrix is imported out of an .csv-file:

seri <- read.table("/PATH/seriation_testdaten.csv", sep="\t", head=TRUE, row.names=1)

This matrix seri contains numeric values like for example a matrix like seritest:

seritest <- matrix(1:100,10)

Now I would like to divide the rows of this matrix into groups. For example, I would like to have groups each with three rows. So one group should contain the rows 1,2 and 3, the next one the rows 4,5 and 6 and so on, until nrow(seri) is reached. It's no problem, if the last group just contains less than three rows.

Matrix 1:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1   11   21   31   41   51   61   71   81    91
[2,]    2   12   22   32   42   52   62   72   82    92 
[3,]    3   13   23   33   43   53   63   73   83    93

Matrix 2:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[4,]    4   14   24   34   44   54   64   74   84    94
[5,]    5   15   25   35   45   55   65   75   85    95
[6,]    6   16   26   36   46   56   66   76   86    96

Matrix 3:

...

That's the first step. But I would like to go further. In this example I have groups of three rows in the resulting submatrices. But I also want the resulting submatrices for groups of 1 row, 2 rows, 4 rows, 5 rows and so on up to nrow(seri) rows. So basically hundreds of submatrices which are each part of a unit based on the decision how big the group-size should be.

Thanks to the help of @farnsy I was able to produce working code, which does exactly what I want:

seritest <- matrix(1:10000,100)

a = nrow(seritest)
e = 1:nrow(seritest)

seri_sub <- list()
U=1
while(U<=a) {
  Q=0
  AQ=0
  EQ=0
  Uk <- U*1000;
  repeat{
    (e[U]*Q)+1 -> EQ;
    Q=Q+1;
    e[U]*Q -> AQ;
    if(AQ>a) break
    seri_sub[[Uk+Q]] <- seritest[EQ:AQ,];
  };
  U=U+1;
}

I can access the matrices by calling for example seri_sub[[3002]]. Like this I get the second (300**2**) group (rows 4,5 and 6) of the unit which is the result of a division of the initial matrix into groups of three (**3**002).

Like already mentioned by @Dason, this code is inefficient. Many list-elements are empty (NULL). Maybe somebody has an idea how to improve it.

This code runs without any further packages. R version 3.0.2 (2013-09-25). OS: Ubuntu 14.04 (64bit). (I'm not a native speaker - please excuse the lack of eloquence)

nevrome
  • 1,471
  • 1
  • 13
  • 28
  • Not sure if this works for you. `lst1 <- setNames(as.list(1:10), LETTERS[1:10]);set.seed(42); s1 <- sample(seq_along(lst1), length(lst1), replace=T);split(lst1, s1)` – akrun Aug 16 '14 at 18:46
  • 3
    Your question is not very clear. Could you explain what kind of list you start with, and what you would like the putput to look like? – SimonG Aug 16 '14 at 18:57
  • Thank you for the fast answers - @akrun Took me a while to get this. But how can I separate the results of this split-function into different list-elements? And what's the purpose of set.seed(42)? – nevrome Aug 16 '14 at 19:04
  • 4
    The `split` function separates it to different lists. `seet.seed` is in order to make it reproducible when using `sample`. If you want a clear answer, please provide a clear question with a reproducible example and desired output – David Arenburg Aug 16 '14 at 19:10

1 Answers1

1

seri doesn't seem like a list here. Neither do A or B, actually. Are you sure you are talking about lists at all? It looks more like you want to subset a matrix a bunch of times, creating submatrices. I can't imagine you actually want "random" names, either. That's crazy talk.

If you want to break up a matrix, why not store all the resulting matrices in an actual list?

myList <- list()
myList[[1]] <- seri[a:b,]
myList[[2]] <- seri[c:d,]

You can see how it would be pretty easy to put this in a loop. Now myList is a list of matrices and, for example, mylist[[i]] would be the i-th matrix. If you want the second row and third column entry, it would be mylist[[i]][2,3].

farnsy
  • 2,282
  • 19
  • 22
  • Ok - this seems to be exactly what I've been searching for. seri is in fact a matrix, not a list. R terminology. I didn't know, that it is possible to store matrices in a list. Thanks - I'll use this. – nevrome Aug 16 '14 at 20:26