Re: [R] Subsetting a matrix without for-loop

From: jim holtman <jholtman_at_gmail.com>
Date: Tue 31 Jan 2006 - 03:33:45 EST

use 'filter':
> x <- matrix(1:100,10)
> x

      [,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
 [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
 [7,]    7   17   27   37   47   57   67   77   87    97
 [8,]    8   18   28   38   48   58   68   78   88    98
 [9,]    9   19   29   39   49   59   69   79   89    99
[10,] 10 20 30 40 50 60 70 80 90 100
> (y <- apply(x, 2, filter, c(1,1,1)))

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]

 [1,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
 [2,]    6   36   66   96  126  156  186  216  246   276
 [3,]    9   39   69   99  129  159  189  219  249   279
 [4,]   12   42   72  102  132  162  192  222  252   282
 [5,]   15   45   75  105  135  165  195  225  255   285
 [6,]   18   48   78  108  138  168  198  228  258   288
 [7,]   21   51   81  111  141  171  201  231  261   291
 [8,]   24   54   84  114  144  174  204  234  264   294
 [9,]   27   57   87  117  147  177  207  237  267   297
[10,] NA NA NA NA NA NA NA NA NA NA
> y[-c(1, nrow(y)),]

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]

[1,]    6   36   66   96  126  156  186  216  246   276
[2,]    9   39   69   99  129  159  189  219  249   279
[3,]   12   42   72  102  132  162  192  222  252   282
[4,]   15   45   75  105  135  165  195  225  255   285
[5,]   18   48   78  108  138  168  198  228  258   288
[6,]   21   51   81  111  141  171  201  231  261   291
[7,]   24   54   84  114  144  174  204  234  264   294
[8,]   27   57   87  117  147  177  207  237  267   297

>

On 1/30/06, Camarda, Carlo Giovanni <Camarda@demogr.mpg.de> wrote:
>
> Dear R-users,
> I'm struggling in R in order to "squeeze" a matrix without using a
> for-loop.
> Although my case is a bit more complex, the following example should
> help you to understand what I would like to do, but without the slow
> for-loop.
> Thanks in advance,
> Carlo Giovanni Camarda
>
>
> A <- matrix(1:54, ncol=6) # my original matrix
> A.new <- matrix(nrow=3, ncol=6) # a new matrix which I'll fill
> # for-loop
> for(i in 1:nrow(A.new)){
> B <- A[i:(i+2), ] # selecting the rows
> C <- apply(B,2,sum) # summing by columns
> A.new[i,] <- C # inserting in the new matrix
> }
>
>
> +++++
> This mail has been sent through the MPI for Demographic Rese...{{dropped}}
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

--
Jim Holtman
Cincinnati, OH
+1 513 247 0281

What the problem you are trying to solve?

	[[alternative HTML version deleted]]

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Tue Jan 31 03:44:55 2006

This archive was generated by hypermail 2.1.8 : Tue 31 Jan 2006 - 08:22:51 EST