# [R-sig-Geo] Calculating distance to nest with spDistsN1

St John Brown st_john_brown at yahoo.com
Tue Sep 30 03:04:08 CEST 2014

```Anna-Marie,

If you look at the documentation for spDistsN1 (i.e. run "?spDistsN1"), you will see that the second argument is suppose to be a single 2D point, and not many 2D points. As per the documentation descripttion, "the function returns a vector of distances between a matrix of 2D points ... and a single 2D point."

I am not 100% sure I understand what you are trying to do, but I think what you are looking for is the spDists function. Compare the results of my output from using spDistsN1 versus spDists.

I hope this helps.

St. John

make_locations = function(n){
lon = runif(n=n,min=7,max=8)
lat = runif(n=n,min=50,max=60)
locations = matrix(append(lon, lat), nrow=n, ncol=2)
colnames(locations) = c("lon", "lat")
rownames(locations) = 1:n
return(locations)
}

set.seed(1)
flight_locations = make_locations(n=3)
nest_locations = make_locations(n=3)

flight_locations
nest_locations

spDistsN1(flight_locations, nest_locations[1,], longlat=T)
spDists(flight_locations, nest_locations, longlat=T)

On Monday, September 29, 2014 5:47 AM, Anna-Marie Corman <annajess at gmx.de> wrote:

Dear list,

I have a question regarding the spDistsN1 function. I want to calculate
the distance of each flight position to the relevant nest position. My
dataset consists of several birds with a differing number of trips.
There is a unique trip_id with the first number regarding to the bird
and the second number regarding to the trip.

coordinates       Date     Time Speed Direction col_lat col_long dist_col
1 (7.87248, 54.1866) 04.07.2014 09:25:00  0.02    314.57 54.1866   7.8724      0.0
2 (7.87148, 54.1869) 04.07.2014 09:30:00 19.11      1.31 54.1866   7.8724      0.1
3  (7.87166, 54.185) 04.07.2014 09:35:00 14.98    224.79 54.1866   7.8724      0.2
4 (7.88635, 54.1569) 04.07.2014 09:40:00 11.44    136.97 54.1866   7.8724      3.4
5 (7.92096, 54.1363) 04.07.2014 09:45:00  0.28    262.14 54.1866   7.8724      6.4
6  (7.91931, 54.136) 04.07.2014 09:50:00  0.42    218.89 54.1866   7.8724      6.4
trip_no bird_id trip_id                  DT           date_time
1       1       1     1.1 04.07.2014 09:25:00 2014-07-04 09:25:00
2       1       1     1.1 04.07.2014 09:30:00 2014-07-04 09:30:00
3       1       1     1.1 04.07.2014 09:35:00 2014-07-04 09:35:00
4       1       1     1.1 04.07.2014 09:40:00 2014-07-04 09:40:00
5       1       1     1.1 04.07.2014 09:45:00 2014-07-04 09:45:00
6       1       1     1.1 04.07.2014 09:50:00 2014-07-04 09:50:00

Formerly, when I only had one bird and so only one nest position (here
col_lat & col_long), I did the following:

spDistsN1(coordinates(dat at coords), matrix(c(8.3495235,54.7042698), nrow
= 1), longlat = T)

and this worked fine.

But now I want to calculate the distance from nest for each bird
simultaneously. Must be simple, but I do not get it. I thought I could
simply use

spDistsN1(coordinates(dat at coords), matrix(c(dat\$col_long,dat\$col_lat),
nrow = 1), longlat = T)

instead, but it does not work.

Do you have any idea how to solve the "problem"?

Thanks a lot in advance.

Best,
Anna

[[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

```