Re: [R] Matrix indexing in a loop

2006-02-18 Thread Gabor Grothendieck
There was an error in the function f -- it only worked correctly if order was 1. Here it is with that fixed. The function f is the only thing changed from my last post. It makes use of the fact that sum(x) == n and sum(x*x) == n*n only occur when one element of x is n and the rest are 0. root3

Re: [R] Matrix indexing in a loop

2006-02-17 Thread Gabor Grothendieck
Thought about this some more and here is a solution that works with 2d and 3d (and higher dimensions). inner is a generalized inner product similar to a function I have posted previously and f(x,y) is an inner product such that f(x,y) is TRUE if abs(x - y) == order (after converting both x and y t

Re: [R] Matrix indexing in a loop

2006-02-17 Thread Gabor Grothendieck
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 th

Re: [R] Matrix indexing in a loop

2006-02-17 Thread Pontarelli, Brett
Jason -Original Message- From: Pontarelli, Brett [mailto:[EMAIL PROTECTED] Sent: Friday, February 17, 2006 4:04 PM To: Mills, Jason; r-help@stat.math.ethz.ch Subject: RE: [R] Matrix indexing in a loop Do you have to use a loop? The following function should do what you want for the

Re: [R] Matrix indexing in a loop

2006-02-17 Thread Pontarelli, Brett
Mills, Jason Sent: Friday, February 17, 2006 1:36 PM To: r-help@stat.math.ethz.ch Subject: [R] Matrix indexing in a loop 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,

[R] Matrix indexing in a loop

2006-02-17 Thread Mills, Jason
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,]159 [2,]