Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Calculate distance between one individual to every predator on same date

$
0
0

I am calculating the distance between moose and wolves using UTM33 coordinates over the period of 2 years (large dataset) in R. I want to have a final table that gives me something like mooseID mooseDate mooseX mooseY wolfID wolfDate wolfX wolfY Distance(m). So far I think I've been able to calculate the distance, but can't seem to match the dates (for example, for every moose position, I want to find the distance of all the wolves at that moment). Also I have more data for the moose (40 individuals) than the wolves (10 individuals) and moose locations are recorded every 2h whereas wolf locations are recorded every 4h. All I'm interested to have right now is to see within a same day the distance of every moose to every wolf and later to filter encounters that are <1000m (I'd like to see all wolves that were close to the moose that day, and not just the closest).

How can I change my code so that dates match? Here is the code I have so far.

#Change to data.table and sort date format
mloc = data.table(moosesubset)
wloc = data.table(wolfsubset)
mloc$DateTime=as.POSIXct(mloc$DateTime, format = "%d.%m.%Y %H:%M:%S")
wloc$DateTime=as.POSIXct(wloc$DateTime, format = "%d.%m.%Y %H:%M:%S")

#Sort by date
Sortmoose = mloc[order(as.Date(mloc$DateTime,format="%d.%m.%Y %H:%M:%S")),]
Sortwolf = wloc[order(as.Date(wloc$DateTime,format="%d.%m.%Y %H:%M:%S")),]

# Try distances only
moosewolf=cbind(Sortmoose,Sortwolf)
moosewolf$distance= sqrt(((moosewolf[,12]-moosewolf[,5])^2)+(moosewolf[,13]-moosewolf[,6])^2)

names(moosewolf)=c("Id","MooseID","Moose DateTime","Moose X", "Moose Y","Moose Altitude",
                   "Id", "Wolf ID", "Wolf DateTime", "Wolf X", "Wolf Y", "Wolf Altitude",
                   "Distance(m)")                      
moosewolf$`Distance(m)`= round(moosewolf$`Distance(m)`)
encounters=moosewolf[which(moosewolf$`Distance(m)`<1000)]

Sample of sorted moosewolf data: [Data][1]

Id MooseID Moose DateTime Moose X Moose Y Moose Altitude Id Wolf ID Wolf DateTime Wolf X Wolf Y Wolf Altitude Distance(m)

1 49584637 E1801 22-02-18 0:00 363062 6757655 251 49589936 M18-12 22-02-18 12:00 355361 6754244 305 8423

2 49585438 E1802 22-02-18 0:00 361651 6738182 410 49589999 M18-12 22-02-18 13:00 355568 6754157 298 17094

3 49579657 E1803 22-02-18 0:00 362825 6755879 258 49589933 M18-12 22-02-18 14:00 355792 6754679 314 7135

4 49586561 E1804 22-02-18 0:00 366561 6749456 437 49589995 M18-12 22-02-18 15:00 355792 6754691 303 11974

5 49590435 E1806 22-02-18 0:00 368068 6756278 304 49589929 M18-12 22-02-18 16:00 354580 6755783 331 13497

6 49584613 E1807 22-02-18 0:00 364385 6759082 244 49589992 M18-12 22-02-18 17:00 354659 6756301 351 10116

7 49582430 E1808 22-02-18 0:00 375119 6764880 551 49589990 M18-12 22-02-18 18:00 354659 6756306 350 22184

8 49582803 E1809 22-02-18 0:00 380085 6757936 427 49589988 M18-12 22-02-18 19:00 354655 6756301 351 25483

9 49584608 E1801 22-02-18 1:00 363058 6757655 248 49589987 M18-12 22-02-18 20:00 354655 6756296 340 8512

10 49584624 E1801 22-02-18 2:00 363055 6757639 278 49589918 M18-12 22-02-18 21:00 354655 6756302 353 8506

11 49584623 E1801 22-02-18 3:00 363119 6757570 315 49589980 M18-12 22-02-18 22:00 354621 6756376 354 8581

12 49584621 E1801 22-02-18 4:00 363164 6757746 303 49589890 M18-12 22-02-18 23:00 354638 6756203 358 8664

13 49584615 E1801 22-02-18 5:00 363152 6757686 247 49589883 M18-12 23-02-18 0:00 354881 6755596 328 8531

14 49584590 E1801 22-02-18 6:00 363152 6757698 252 49586495 M18-13 22-02-18 12:00 355335 6754240 308 8548

15 49584562 E1801 22-02-18 7:00 363133 6757697 219 49586492 M18-13 22-02-18 13:00 355457 6754213 304 8430

Thanks!


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>