[R] Matrix indexing in a loop
Gabor Grothendieck
ggrothendieck at gmail.com
Sat Feb 18 03:09:15 CET 2006
For 2d here is a solution based on zoo. It turns the matrix into
a time series and lags it forwards and backwards and does the
same for its transpose in order to avoid index machinations.
The function is called rook2 and it first defines three local
functions, one that converts NAs to zero, one that does
a lag using na.pad = TRUE and one to invoke Lag and
add up the lags:
library(zoo)
rook2 <- function(x, i = 1) {
na2zero <- function(x) ifelse(is.na(x), 0, x)
Lag <- function(x, i) na2zero(lag(zoo(x), i, na.pad = TRUE))
Rook <- function(x, i) Lag(x, i) + Lag(x, -i) + t(Lag(t(x), i) +
Lag(t(x), -i))
Rook(x, i) / Rook(1+0*x, i)
}
# test
m <- matrix(1:24, 6)
rook2(m)
On 2/17/06, Mills, Jason <Jason.Mills at afhe.ualberta.ca> wrote:
>
> How do you specify matrix location a[i,j] (or a[i-1,j], etc.) in a "for"
> loop?
>
> I am looking for a flexible method of indexing neighbors over a series
> of lags (1,2,3...) and I may wish to extend this method to 3D arrays.
>
>
> Example:
>
> Data matrix
> > fun
> [,1] [,2] [,3]
> [1,] 1 5 9
> [2,] 2 6 10
> [3,] 3 7 11
> [4,] 4 8 12
>
>
> For each element a[i,j] in "fun", sum the 1st order (Rook's) neighbors:
>
> a[i-1,j]
>
> a[i+1,j]
>
> a[i,j-1]
>
> a[i,j+1]
>
> Then divide by the number of elements included as neighbors-- this
> number depends on the location of a[i,j] in the matrix.
>
>
> Insert the product of the neighbor calculation for each a[i,j] into the
> corresponding position b[i,j] in an empty matrix with the same
> dimensions as "fun".
>
>
> For example, element [2,2] in "fun" should yield element [2,2] in a new
> matrix equal to 24/4=6. Of course, element [1,1] in the new matrix
> should be the product of only two numbers.
>
>
> Thanks
>
> J. Mills
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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
>
More information about the R-help
mailing list