R 在数据帧中按行程计算距离

R 在数据帧中按行程计算距离,r,location,distance,latitude-longitude,distribution,R,Location,Distance,Latitude Longitude,Distribution,我有一个数据框,其中有一只企鹅在福克兰群岛进行了46次旅行,我计算了几个参数,假设每次觅食旅行都是在企鹅距离海岸1000米以上的时候,当它回到时停止,我自由地从零开始创建了一个解决方案 library(lubridate) library(data.table) dt<-data.table( date=ymd_hms(c("2014-06-11 19:02:00" ,"2014-06-12 01:02:00" ,"2014-06-

我有一个数据框,其中有一只企鹅在福克兰群岛进行了46次旅行,我计算了几个参数,假设每次觅食旅行都是在企鹅距离海岸1000米以上的时候,当它回到时停止,我自由地从零开始创建了一个解决方案

library(lubridate)
library(data.table)

dt<-data.table(
  date=ymd_hms(c("2014-06-11 19:02:00"
             ,"2014-06-12 01:02:00"
             ,"2014-06-12 13:02:00"
             ,"2014-06-12 19:02:00"
             ,"2014-06-13 01:02:00"
             ,"2014-06-13 07:02:00"
             ,"2014-06-13 13:02:00"
             ,"2014-06-13 19:02:00"
             ,"2014-06-14 01:02:00"
             ,"2014-06-14 13:02:00"
             ,"2014-06-14 19:02:00"
             ,"2014-06-15 01:02:00"
             ,"2014-06-15 13:02:00"
             ,"2014-06-15 19:02:00"
             ,"2014-06-16 01:02:00"
             ,"2014-06-16 07:02:00"
             ,"2014-06-16 13:02:00"
             ,"2014-06-16 19:02:00"
             ,"2014-06-17 01:02:00"
             ,"2014-06-17 07:02:00"
             ,"2014-06-17 13:02:00"
             ,"2014-06-17 19:02:00"
             ,"2014-06-18 01:02:00"
             ,"2014-06-18 07:02:00"
             ,"2014-06-18 13:02:00"
             ,"2014-06-18 19:02:00"
             ,"2014-06-19 01:02:00"
             ,"2014-06-19 07:02:00"
             ,"2014-06-19 13:02:00"
             ,"2014-06-19 19:02:00"))
  ,lon=c(-58.3508585 
         ,-58.35589725
         ,-58.27224941
         ,-58.27974654
         ,-58.32331901
         ,-58.35833139
         ,-58.35617673
         ,-58.34055711
     ,-58.34982536
     ,-58.3073814 
     ,-58.34581314
     ,-58.34050624
     ,-58.2974691 
     ,-58.19881901
     ,-58.1348416 
     ,-57.99399544
     ,-58.00469754
     ,-57.92758675
     ,-57.91658235
     ,-57.77015528
     ,-57.99601712
     ,-58.06820013
     ,-58.19845185
     ,-58.35241361
     ,-58.35603546
     ,-58.33350332
     ,-58.33839581
     ,-58.42661519
     ,-58.30461883
     ,-58.18362875)
  ,lat=c(-51.88438373 
     ,-51.88349529 
     ,-51.91903677 
     ,-51.90535003 
     ,-51.89410464 
     ,-51.88809227 
     ,-51.88156281 
     ,-51.89002367 
     ,-51.8715761  
     ,-51.92722937 
     ,-51.86761133 
     ,-51.88382088 
     ,-51.91795326 
     ,-51.95172233 
     ,-51.98673766 
     ,-52.06988191 
     ,-52.02795069 
     ,-52.02184666 
     ,-51.99748699 
     ,-51.99031797 
     ,-51.91519551 
     ,-51.92972737 
     ,-51.89522513 
     ,-51.88015998 
     ,-51.88336878 
     ,-51.87308427 
     ,-51.87846631 
     ,-51.80902388 
     ,-51.93745837 
     ,-51.96475914 )
  ,trip=c( 1 
       , 1 
       , 1 
       , 1 
       , 1 
       , 1 
       , 1 
       , 1 
       , 2 
       , 2 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 4 
       , 4 
       , 4 )
  ,distance2coast=c(2236.067977
                ,1000
                ,7211.102551
                ,5830.951895
                ,3605.551275
                ,1414.213562
                ,1000
                ,2236.067977
                ,1000
                ,7071.067812
                ,1000
                ,1414.213562
                ,6324.55532
                ,13000
                ,18788.29423
                ,28861.73938
                ,26627.05391
                ,29000
                ,28284.27125
                ,30805.8436
                ,17804.49381
                ,14866.06875
                ,7615.773106
                ,1000
                ,1000
                ,1000
                ,1414.213562
                ,0
                ,7810.249676
                ,14317.82106)
屈服于:

$trip1
[1] 531.6949

$trip2
[1] 354.8975

$trip3
[1] 3067.865

$trip4
[1] 1012.005

我冒昧地从零开始创建了一个解决方案

library(lubridate)
library(data.table)

dt<-data.table(
  date=ymd_hms(c("2014-06-11 19:02:00"
             ,"2014-06-12 01:02:00"
             ,"2014-06-12 13:02:00"
             ,"2014-06-12 19:02:00"
             ,"2014-06-13 01:02:00"
             ,"2014-06-13 07:02:00"
             ,"2014-06-13 13:02:00"
             ,"2014-06-13 19:02:00"
             ,"2014-06-14 01:02:00"
             ,"2014-06-14 13:02:00"
             ,"2014-06-14 19:02:00"
             ,"2014-06-15 01:02:00"
             ,"2014-06-15 13:02:00"
             ,"2014-06-15 19:02:00"
             ,"2014-06-16 01:02:00"
             ,"2014-06-16 07:02:00"
             ,"2014-06-16 13:02:00"
             ,"2014-06-16 19:02:00"
             ,"2014-06-17 01:02:00"
             ,"2014-06-17 07:02:00"
             ,"2014-06-17 13:02:00"
             ,"2014-06-17 19:02:00"
             ,"2014-06-18 01:02:00"
             ,"2014-06-18 07:02:00"
             ,"2014-06-18 13:02:00"
             ,"2014-06-18 19:02:00"
             ,"2014-06-19 01:02:00"
             ,"2014-06-19 07:02:00"
             ,"2014-06-19 13:02:00"
             ,"2014-06-19 19:02:00"))
  ,lon=c(-58.3508585 
         ,-58.35589725
         ,-58.27224941
         ,-58.27974654
         ,-58.32331901
         ,-58.35833139
         ,-58.35617673
         ,-58.34055711
     ,-58.34982536
     ,-58.3073814 
     ,-58.34581314
     ,-58.34050624
     ,-58.2974691 
     ,-58.19881901
     ,-58.1348416 
     ,-57.99399544
     ,-58.00469754
     ,-57.92758675
     ,-57.91658235
     ,-57.77015528
     ,-57.99601712
     ,-58.06820013
     ,-58.19845185
     ,-58.35241361
     ,-58.35603546
     ,-58.33350332
     ,-58.33839581
     ,-58.42661519
     ,-58.30461883
     ,-58.18362875)
  ,lat=c(-51.88438373 
     ,-51.88349529 
     ,-51.91903677 
     ,-51.90535003 
     ,-51.89410464 
     ,-51.88809227 
     ,-51.88156281 
     ,-51.89002367 
     ,-51.8715761  
     ,-51.92722937 
     ,-51.86761133 
     ,-51.88382088 
     ,-51.91795326 
     ,-51.95172233 
     ,-51.98673766 
     ,-52.06988191 
     ,-52.02795069 
     ,-52.02184666 
     ,-51.99748699 
     ,-51.99031797 
     ,-51.91519551 
     ,-51.92972737 
     ,-51.89522513 
     ,-51.88015998 
     ,-51.88336878 
     ,-51.87308427 
     ,-51.87846631 
     ,-51.80902388 
     ,-51.93745837 
     ,-51.96475914 )
  ,trip=c( 1 
       , 1 
       , 1 
       , 1 
       , 1 
       , 1 
       , 1 
       , 1 
       , 2 
       , 2 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 3 
       , 4 
       , 4 
       , 4 )
  ,distance2coast=c(2236.067977
                ,1000
                ,7211.102551
                ,5830.951895
                ,3605.551275
                ,1414.213562
                ,1000
                ,2236.067977
                ,1000
                ,7071.067812
                ,1000
                ,1414.213562
                ,6324.55532
                ,13000
                ,18788.29423
                ,28861.73938
                ,26627.05391
                ,29000
                ,28284.27125
                ,30805.8436
                ,17804.49381
                ,14866.06875
                ,7615.773106
                ,1000
                ,1000
                ,1000
                ,1414.213562
                ,0
                ,7810.249676
                ,14317.82106)
屈服于:

$trip1
[1] 531.6949

$trip2
[1] 354.8975

$trip3
[1] 3067.865

$trip4
[1] 1012.005

我发现从data.frame获取所有信息的最佳方法是使用Adehigtation包。 因此,我使用了名为locs_utm的“空间点数据帧”(与上面我称之为locs1_utm的相同),将其转换为带有“x”和“y”的坐标为列命名,并创建一个名为BirdTrips的列,该列具有唯一的值,因此我可以稍后将其用作突发和拆分,并结合id列和trip列获取每次行程的距离和时间

gent$trip[gent$trip<10]=paste(0,gent$trip[gent$trip<10],sep="")#so that trip 1,2,3 becomes 01,02,03...
gent$BirdTrip=paste(gent$id,gent$trip,sep=".")  #combine the animal id with the trip so you have 1.01,1.02, 1.03 and soon
gent$BirdTrip=as.factor(gent$BirdTrip)

BirdTrip
  1.01
  1.01
  ...
  2.01


foo = as.data.frame(locs_utm) #transform locs_utm-->SpatialPointDataframe into data.frame
foo = cbind.data.frame(foo$coords.x1, foo$coords.x2)  #convert lon lat, columns in coordinatse
names(foo) = c("x", "y")  #convert the columns in "x" and "y" to be able to use the as.ltraj and convert thedata in ltraj forat

tr=as.ltraj(xy=foo,
            date=p.1$date,
            id=p.1$id,
            burst=p.1$BirdTrip)

class(tr)    #[1] "ltraj" "list" 
tr=ld(tr)   #transform the ltraj into data.frame

我发现从data.frame获取所有信息的最佳方法是使用Adehigtation包。 因此,我使用了名为locs_utm的“空间点数据帧”(与上面我称之为locs1_utm的相同),将其转换为带有“x”和“y”的坐标为列命名,并创建一个名为BirdTrips的列,该列具有唯一的值,因此我可以稍后将其用作突发和拆分,并结合id列和trip列获取每次行程的距离和时间

gent$trip[gent$trip<10]=paste(0,gent$trip[gent$trip<10],sep="")#so that trip 1,2,3 becomes 01,02,03...
gent$BirdTrip=paste(gent$id,gent$trip,sep=".")  #combine the animal id with the trip so you have 1.01,1.02, 1.03 and soon
gent$BirdTrip=as.factor(gent$BirdTrip)

BirdTrip
  1.01
  1.01
  ...
  2.01


foo = as.data.frame(locs_utm) #transform locs_utm-->SpatialPointDataframe into data.frame
foo = cbind.data.frame(foo$coords.x1, foo$coords.x2)  #convert lon lat, columns in coordinatse
names(foo) = c("x", "y")  #convert the columns in "x" and "y" to be able to use the as.ltraj and convert thedata in ltraj forat

tr=as.ltraj(xy=foo,
            date=p.1$date,
            id=p.1$id,
            burst=p.1$BirdTrip)

class(tr)    #[1] "ltraj" "list" 
tr=ld(tr)   #transform the ltraj into data.frame


您是否可以编辑您的问题以包含
dput(p.1)
?这样我们就可以帮助您,而无需手动重新创建数据帧。我不想要矩阵,这是我试图用
dput(p.1)粘贴表格时遇到的问题
但它运行的是一个巨大的表,而且我的字符数有限,我在itI上,我现在没有时间完全概括这一点(从而正确地回答您的问题)。您能检查一下这是否表明有任何承诺吗?spDists来自包“sp”很抱歉,我正在尝试剪切表格并使上传更清晰,我对堆栈溢出是新手,过去几天我一直在尝试正确粘贴data.frames,但由于某些原因,我无法粘贴,因为“dput()”会产生巨大的行数,甚至看起来最糟糕。下面是指向excel文件部分的链接:[您能否编辑您的问题以包含
dput(p.1)
?这样我们就可以帮助您,而无需手动重新创建数据帧。我不想要矩阵,这是我试图用
dput(p.1)粘贴表格时遇到的问题。)
但它运行的是一个巨大的表,而且我的字符数有限,我在itI上,我现在没有时间完全概括这一点(从而正确地回答您的问题)。您能检查一下这是否表明有任何承诺吗?spDists来自包“sp”很抱歉,我正在尝试剪切表格并使上传更清晰,我对堆栈溢出是新手,过去几天我一直在尝试正确粘贴data.frames,但由于某些原因,我无法粘贴,因为“dput()”会产生巨大的行数,甚至看起来最糟糕。下面是指向excel文件部分的链接:[地球不是一个完美的球体,那么距离可能会因你所处的位置而有所不同are@Daniel一些测地方法解释了我正在尝试您的代码,当我尝试拆分代码时,我在deparse(…):unused argument(by=“trip”)中收到了下一条消息Error我在这里附上一个.Rda文件,其中记录了我从我的代码中通过spDists代码获得的距离,[.第一次旅行中我看到的距离为16693.9659米。但是我无法处理这些距离,因为这是一个矩阵,我以后无法使用。代码应该从头开始执行。这就是try
rm(list=ls())
并尝试按照上面提供的方式运行整个脚本。确保还包括必要的库(特别是
数据表
)我只是在代码中出现了一个又一个错误,当我使用lappy函数到达最后一行时,它只会进入不起作用的浏览器地球不是一个完美的球体,然后距离可能会因您所在的位置而异are@Daniel一些测地方法解释说,我正在尝试你的代码,当我尝试拆分代码时,我会选择下一个mesdeparse中的sage错误(…):未使用的参数(by=“trip”)我在这里附上一个.Rda文件,其中包含我从代码中使用spDists代码获得的距离,[.第一次旅行中我看到的距离为16693.9659米。但我无法处理这些距离,因为这是一个矩阵,我以后无法使用。代码应该从头开始执行。也就是说,请尝试
rm(list=ls())
,并尝试按照上面提供的方式运行整个脚本。确保还包括必要的库(特别是
data.table
)我只是在代码中出现了一个又一个错误,当我使用lappy函数到达最后一行时,它只会进入无法工作的浏览器
dt_split<-split(dt,by="trip")
names(dt_split)<-paste0("trip",names(dt_split))#and we give name to each element
calcDistancePerTrip<-function(y){#y is dataframe-element of list
  l1<-lapply(seq_along(1:nrow(y)),function(x){
    dist<-distanceOfPoints(long1 = y[,.(lon)][x],long2= y[,.(lon)][x+1],lat1 = y[,.(lat)][x],lat2 = y[,.(lat)][x+1])
    #you can always use the gcd.hf for the dist calculation
    return(dist)
  })
  l1<-as.numeric(unlist(l1))#list to numeric vector
  tot_dist<-sum(l1,na.rm = T)#cumulative distance per trip 
  return(tot_dist)
}
lapply(dt_split,calcDistancePerTrip)
$trip1
[1] 531.6949

$trip2
[1] 354.8975

$trip3
[1] 3067.865

$trip4
[1] 1012.005
gent$trip[gent$trip<10]=paste(0,gent$trip[gent$trip<10],sep="")#so that trip 1,2,3 becomes 01,02,03...
gent$BirdTrip=paste(gent$id,gent$trip,sep=".")  #combine the animal id with the trip so you have 1.01,1.02, 1.03 and soon
gent$BirdTrip=as.factor(gent$BirdTrip)

BirdTrip
  1.01
  1.01
  ...
  2.01


foo = as.data.frame(locs_utm) #transform locs_utm-->SpatialPointDataframe into data.frame
foo = cbind.data.frame(foo$coords.x1, foo$coords.x2)  #convert lon lat, columns in coordinatse
names(foo) = c("x", "y")  #convert the columns in "x" and "y" to be able to use the as.ltraj and convert thedata in ltraj forat

tr=as.ltraj(xy=foo,
            date=p.1$date,
            id=p.1$id,
            burst=p.1$BirdTrip)

class(tr)    #[1] "ltraj" "list" 
tr=ld(tr)   #transform the ltraj into data.frame
ts = as.data.frame(as.POSIXct(tapply(tr$date, tr$burst, min, na.rm = T), origin = "1970-01-01")) #trip start
te = as.data.frame(as.POSIXct(tapply(tr$date, tr$burst, max, na.rm = T), origin = "1970-01-01")) #trip end
th = as.data.frame(tapply(tr$dt, tr$burst, sum, na.rm = T)/3600) #trip hours
tl = as.data.frame(tapply(tr$dist, list(tr$burst), sum, na.rm = T)/1000) #trip kms-->LENGTH
td = as.data.frame(sqrt(tapply(tr$R2n, list(tr$burst), max, na.rm = T))/1000) #max displacement kms

resumen = cbind.data.frame(ts[,1], te[,1], as.numeric(th[,1]), as.numeric(tl[,1]), as.numeric(td[,1]))
names(resumen) = c("Start", "End", "Hours", "Length", "Displacement")
View(resumen)