[R] distance between consecutive points
David Winsemius
dwinsemius at comcast.net
Wed Feb 16 04:05:45 CET 2011
On Feb 15, 2011, at 8:22 PM, Peter Alspach wrote:
> Tena koe D'Arcy
>
> You might find dist() more suited to your needs.
>
> HTH ....
>
> Peter Alspach
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> project.org] On Behalf Of Darcy Webber
>> Sent: Wednesday, 16 February 2011 1:10 p.m.
>> To: r-help at r-project.org
>> Subject: [R] distance between consecutive points
>>
>> Dear R users,
>>
>> I have two coloumns of data, say x and y, referring to a list of
>> points in 2D space. I am trying to develop a code that will give me
>> the distances (using Pythagoras) between consecutive points (xi,yi)
>> and (xi+1,yi+1). So far I have come up with the following:
>>
>> for (i in 1:length(x)) d<-sqrt((x[i+1]-x[i])^2+(y[i+1]-y[i])^2)
Or try:
for (i in 1:(length(x)-1) ) d<-sqrt((x[i+1]-x[i])^2+(y[i+1]-y[i])^2)
> d
[1] 0.01
However if you try to generalize this you should to something that
will allow successive values do not get overwritten. Rather than using
d try setting d <- vector("numeric", length(x)-1), the assing to d[i]:
for (i in 1:(length(x)-1) ) d[i]<-sqrt((x[i+1]-x[i])^2+(y[i+1]-y[i])^2)
--
David.
>>
>> For example, if I use the two points (note, I have hundreds of points
>> for x,y)
>> x<-c(64.59,64.60)
>> y<-c(-179.28,-179.28)
>>
>> d should be 0.01.
>>
>> But it just doesn't give me the correct answer and I can't figure out
>> why. Any help would be much appreciated.
>>
>> Cheers,
>> D'Arcy
>>
>> ______________________________________________
>> 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.
>
> The contents of this e-mail are confidential and may be subject to
> legal privilege.
> If you are not the intended recipient you must not use, disseminate,
> distribute or
> reproduce all or any part of this e-mail or attachments. If you
> have received this
> e-mail in error, please notify the sender and delete all material
> pertaining to this
> e-mail. Any opinion or views expressed in this e-mail are those of
> the individual
> sender and may not represent those of The New Zealand Institute for
> Plant and
> Food Research Limited.
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list