From: Benilton Carvalho <bcarvalh_at_jhsph.edu>

Date: Wed, 23 Jan 2008 11:54:53 -0500

R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 23 Jan 2008 - 16:58:07 GMT

Date: Wed, 23 Jan 2008 11:54:53 -0500

i'm not so sure i understood, but you might want something in the lines of:

z <- outer(x, x, "-")

(abs(z)>step)*outer(1:length(x), 1:length(x))*z

On Jan 23, 2008, at 11:32 AM, papagenic wrote:

*>
*

> dear experts,

*>
**> I am new to R and am trying to compute a vector y from a vector x
**> where :
**> y[i] = sign(x[j]-x[i])*(j-i) with j the first index after i where
**> abs(x[j]-x[i]) > to a given step
**> y[i] is 0 if there is no such j
**>
**> I can write this in R as follows
**> for(i in 1:length(x)) {
**> y[i]=0
**> for(j in i:length(x)) {
**> if (abs(x[j]-x[i]) > step) {
**> y[i]=sign(x[j]-x[i])*(j-i)
**> break;
**> }
**> }
**> }
**>
**> but I wonder if there is a more efficient way to write this. I
**> understand
**> explicit looping can often be avoided in R using vector notation.
**>
**> Thanks for your help
**> --
**> View this message in context: http://www.nabble.com/newbie%3Alooking-for-an-efficient-way-to-compute-distance-vector-tp15045583p15045583.html
**> Sent from the R help mailing list archive at Nabble.com.
**>
**> ______________________________________________
**> R-help_at_r-project.org mailing list
**> https://stat.ethz.ch/mailman/listinfo/r-help
**> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
**> and provide commented, minimal, self-contained, reproducible code.
*

R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 23 Jan 2008 - 16:58:07 GMT

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Wed 23 Jan 2008 - 22:30:08 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*