查找数据缺口并拆分更多轨道

问题描述

我有一些动物的跟踪数据,我想知道每个ID在卫星传输中是否有超过1天间隔的轨道。

并且,如果有的话,我需要将轨道分为相同ID的2条或更多条子轨道。因此,有些人会有一个以上的爆发。

我的数据是这样的(dput):

structure(list(id = c("20162.03","20162.03","20687.03","21791.03","21791.03"),date = structure(c(1066573980,1066738740,1066743180,1066754280,1066762200,1066905180,1066931520,1067760840,1067765760,1067767740,1067782200,1067274120,1067283840,1067288700,1067292840,1067294460,1067306160,1067307660,1067315100,1067323920,1067333160,1067334780,1067340240,1067342160,1067346360,1067348100,1067384580,1067385060,1067391060,1067393220,1067400660,1067406780,1067413740,1067414580,1067418900,1067419860,1067425380,1067431140,1067433660,1067445780,1067452080,1067459640,1067478840,1067486460,1067492460,1067493780,1067498760,1067499780,1067504700,1067510400,1067516160,1067519160,1067522280,1067531520,1067537640,1067549640,1067549820,1067561160,1067564460,1067567280,1067570340,1067572200,1067578440,1067584680,1067589780,1067590260,1067596320,1067601180,1067604900,1067607300,1067617140,1067623440,1067629440,1067634660,1067640600,1067646360,1067650020,1067652180,1067657940,1067663820,1067669700,1067674680,1067676180,1067680560,1067682000,1067686440,1067690280,1067692440,1067696340,1067702940,1067709120,1067714700,1067719560,1067720400,1067721120,1067725620,1067727060,1067731140,1067735520,1067737140,1067741580,1067743560,1067749620,1067759880,1067760720,1067761620,1067765640,1067767560,1067775960,1067781600,1067788620,1067795040,1067804520,1067805180,1067806740,1067810460,1067812800,1067822280,1067827140,1067829360,1067835480,1067845620,1067850720,1067853480,1067861400,1067862540,1067867580,1067874420,1067880660,1067884320,1067895480,1067898420,1067906460,1067907360,1067915100,1067921040,1067930520,1067935620,1067938800,1067944800,1067946960,1067947500,1067953080,1067953560,1067960280,1067966220,1067975340,1067980560,1067984100,1067986380,1067992380,1067998200,1068000780,1068015300,1068020460,1068024420,1068026520,1068032280,1068038460,1068051900,1068060240,1068065700,1068069720,1068071520,1068077460,1068083220,1068084060,1068092760,1068105660,1068110100,1068111660,1068117780,1068123420,1068124320,1068131760,1068137820,1068145140,1068150600,1068151140,1068155280,1068156480,1068161340,1068162540,1068168480,1068178260,1068190560,1068191160,1068195840,1068196620,1068208620,1068209940,1068215760,1068223680,1068235500,1068236160,1068241020,1068241440,1068254880,1068263940,1068269940,1068276240,1068281580,1068287220,1068293700,1068295260,1068301080,1068309360,1068315060,1068326580,1068326700,1068338580,1068340560,1068346380,1068349680,1068355500,1068360960,1068373020,1068378660,1068380820,1068384720,1068387120,1068395220,1068405900,1068411480,1068412260,1068417480,1068418440,1068423720,1068426180,1068429660,1068431940,1068435120,1068441480,1068451740,1068452040,1068457680,1068458640,1068463620,1068466380,1068469680,1068472380,1068480540,1068486720,1068490860,1068496440,1068498060,1068511800,1068514740,1068521100,1068527280,1068542400,1068544260,1068548880,1068558180,1068566400,1068572580,1068575820,1068581400,1068587280,1068589500,1068593820,1068599700,1068603120,1068606840,1068612840,1068622020,1068627540,1068629940,1068639780,1068666360,1068684720,1068688980,1068698640,1068706800,1068712440,1068715500,1068724920,1068729120,1068730680,1068743820,1068757500,1068760800,1068763440,1068766800,1068769920,1068774300,1068775740,1068784260,1068801240,1068810060,1068816060,1068829620,1068836700,1068842460,1068846540,1068848280,1068882540,1068886860,1068888540,1068901080,1068915420,1068921480,1068927420,1068927600,1068932280,1068933360,1068939720,1068945420,1068945780,1068955800,1068972540,1068973440,1068978660,1068985740,1068991980,1069001100,1069012320,1069017840,1069018380,1069041480,1069047480,1069052580,1069058700,1069064040,1069071420,1069077420,1069103400,1069109460,1069115880,1069116600,1067361120,1067368740,1067373660,1067378340,1067379480,1067400540,1067413800,1067419020,1067419620,1067431080,1067433420,1067445960,1067476140,1067478960,1067492220,1067493840,1067504580,1067519100,1067531820,1067549460,1067561280,1067566980,1067572020,1067584740,1067589600,1067596140,1067601420,1067605020,1067617440,1067623200,1067629500,1067634480,1067646180,1067649780,1067652060,1067657880,1067664000,1067669820,1067676120,1067692380,1067714460,1067719740,1067727120,1067731260,1067737320,1067741340,1067743500,1067749500,1067775840,1067777280,1067781900,1067788800,1067794920,1067810520,1067812680,1067822340,1067827080,1067829420,1067835360,1067884200,1067895540,1067898240,1067907180,1067912580,1067921100,1067930640,1067935800,1067938920,1067947200,1067947620,1067953140,1067960100,1067966460,1067975160,1067977860,1067980440,1067992320,1067998380,1068007020,1068015540,1068020700,1068024540,1068032700,1068038640,1068051960,1068060120,1068065520,1068069540,1068071400,1068077280,1068083340,1068083700,1068086700,1068105720,1068110340,1068111720,1068123720,1068137700,1068145080,1068150240,1068150960,1068155160,1068156360,1068168360,1068169320,1068178320,1068195960,1068196680,1068208800,1068209760,1068223440,1068235920,1068240840,1068241500,1068246780,1068253380,1068255000,1068264000,1068270000,1068287580,1068293640,1068295320,1068301440,1068309240,1068326400,1068326640,1068332520,1068338400,1068340380,1068344640,1068346560,1068349740,1068355800,1068361080,1068366600,1068367200,1068373200,1068378720,1068380760,1068384780,1068386880,1068395040,1068400920,1068411540,1068418260,1068423540,1068426060,1068429480,1068435420,1068441540,1068452280,1068457560,1068458760,1068463800,1068466500),class = c("POSIXct","POSIXt"),tzone = "GMT"),lc = structure(c(4L,1L,2L,3L,4L,6L,5L,7L,3L),.Label = c("1","A","B","Tagging","0","2","3","N","P","Z"),class = "factor"),lon = c(-39.37,-38.517,-38.464,-38.461,-38.322,-38.674,-38.957,-42.084,-41.999,-42.035,-42.14,-39.32,-39.306,-39.301,-39.302,-39.234,-39.215,-39.16,-39.179,-39.375,-39.185,-39.187,-39.154,-39.197,-39.149,-39.214,-39.186,-39.165,-39.182,-39.184,-39.192,-39.21,-39.183,-39.2,-39.202,-39.093,-39.157,-39.268,-39.227,-39.284,-39.376,-39.437,-39.455,-39.46,-39.463,-39.457,-39.429,-39.365,-39.453,-39.466,-39.505,-39.493,-39.472,-39.522,-39.477,-39.444,-39.461,-39.465,-39.418,-39.377,-39.421,-39.338,-39.248,-39.265,-39.528,-39.256,-39.222,-39.198,-39.178,-39.177,-39.142,-39.162,-39.181,-39.188,-39.118,-39.13,-39.132,-39.085,-39.07,-39.037,-39.018,-39.003,-38.965,-38.904,-38.866,-38.891,-38.848,-38.826,-38.811,-38.781,-38.814,-38.82,-38.795,-38.775,-38.715,-38.763,-38.74,-38.701,-38.742,-38.722,-38.714,-38.744,-38.68,-38.671,-38.662,-38.653,-38.589,-38.575,-38.541,-38.534,-38.465,-38.44,-38.437,-38.417,-38.411,-38.443,-38.34,-38.328,-38.236,-38.22,-38.306,-38.231,-38.207,-38.249,-38.259,-38.283,-38.272,-38.307,-38.312,-38.324,-38.359,-38.356,-38.412,-38.493,-38.543,-38.6,-38.608,-38.658,-38.692,-38.736,-38.784,-38.806,-38.81,-38.469,-38.816,-38.874,-38.926,-38.905,-38.869,-38.912,-38.95,-38.911,-38.953,-39.112,-38.945,-38.93,-38.932,-38.946,-38.96,-38.972,-38.808,-38.993,-39.008,-39.027,-39.005,-39.006,-38.986,-38.931,-38.942,-38.944,-38.963,-38.949,-38.981,-39.001,-38.951,-38.974,-38.94,-38.925,-38.883,-38.773,-38.757,-38.737,-38.723,-38.728,-38.707,-38.699,-38.785,-38.708,-38.675,-38.684,-38.648,-38.682,-38.694,-38.743,-38.789,-38.79,-38.798,-38.801,-38.678,-38.591,-38.628,-38.52,-38.525,-38.492,-38.458,-38.456,-38.438,-38.507,-38.357,-38.092,-38.277,-38.274,-38.21,-38.196,-38.18,-38.146,-38.116,-38.015,-38.067,-38.023,-38.03,-38.006,-38.027,-38.038,-38.065,-38.078,-38.137,-38.087,-38.06,-38.239,-38.341,-38.372,-38.112,-38.498,-38.518,-38.683,-38.633,-38.638,-38.703,-38.78,-38.922,-38.992,-38.973,-38.999,-38.979,-39.059,-39.019,-39.02,-39,-38.969,-38.954,-38.91,-38.916,-38.948,-38.983,-38.929,-39.305,-38.854,-38.721,-38.812,-38.759,-38.687,-38.681,-38.689,-38.473,-38.436,-38.327,-38.227,-38.214,-38.232,-38.205,-38.149,-38.09,-38.083,-38.097,-38.075,-37.912,-38.085,-37.948,-38.103,-38.081,-38.12,-38.069,-38.142,-38.126,-38.14,-38.157,-38.133,-38.141,-38.077,-38.127,-39.48,-39.441,-39.45,-39.346,-39.392,-39.271,-39.252,-39.243,-39.283,-39.089,-39.161,-39.078,-39.052,-39.022,-39.047,-38.989,-38.886,-38.69,-38.583,-38.53,-38.434,-38.391,-38.364,-38.414,-38.385,-38.444,-38.424,-38.513,-38.523,-38.557,-38.574,-38.603,-38.719,-38.705,-38.711,-38.758,-38.596,-38.77,-38.807,-38.799,-38.832,-38.882,-38.887,-38.92,-39.038,-39.194,-39.212,-39.296,-39.347,-39.382,-39.335,-39.675,-39.654,-39.699,-39.747,-39.773,-39.794,-39.853,-39.863,-39.901,-39.898,-39.899,-39.871,-39.86,-39.952,-39.914,-39.881,-39.922,-39.996,-40.039,-40.044,-40.106,-40.186,-40.175,-40.202,-40.258,-40.296,-40.434,-40.481,-40.485,-40.478,-40.49,-40.496,-40.483,-40.499,-40.511,-40.624,-40.678,-40.726,-40.783,-40.76,-40.762,-40.748,-40.751,-40.765,-40.764,-40.745,-40.662,-40.692,-40.684,-40.784,-40.819,-41.012,-41.188,-41.251,-41.246,-41.287,-41.386,-41.385,-41.441,-41.443,-41.397,-41.392,-41.339,-41.344,-41.329,-41.303,-41.343,-41.17,-41.163,-41.14,-41.149,-41.162,-41.192,-41.185,-41.2,-41.186,-41.268,-41.213,-41.214,-41.263,-41.243,-41.197,-41.195,-41.23,-41.245,-41.205,-41.226,-41.259,-41.247,-41.221,-41.228,-41.229,-41.216,-41.138,-41.136,-41.123,-41.113,-41.156,-41.176,-41.148,-41.139,-41.078
    ),lat = c(-18.48,-18.253,-18.302,-18.425,-18.512,-19.824,-19.511,-24.071,-24.114,-24.152,-24.38,-18.46,-18.454,-18.458,-18.475,-18.465,-18.433,-18.443,-18.414,-18.356,-18.324,-18.34,-18.363,-18.318,-18.345,-18.315,-18.33,-18.369,-18.366,-18.371,-18.386,-18.392,-18.393,-18.367,-18.37,-18.331,-18.36,-18.357,-18.352,-18.334,-18.382,-18.41,-18.43,-18.439,-18.442,-18.446,-18.463,-18.473,-18.461,-18.523,-18.531,-18.564,-18.553,-18.557,-18.567,-18.598,-18.597,-18.575,-18.56,-18.546,-18.652,-18.533,-18.49,-18.492,-18.453,-18.448,-18.328,-18.387,-18.351,-18.309,-18.285,-18.22,-18.179,-18.159,-18.123,-18.096,-18.061,-18.041,-18.021,-18.042,-18.008,-18.026,-18.029,-18.039,-18.045,-18.074,-18.064,-18.075,-18.097,-18.114,-18.103,-18.107,-18.059,-18.046,-18.057,-18.024,-18.005,-18,-17.899,-18.011,-18.017,-18.056,-18.051,-18.06,-18.186,-18.05,-17.996,-17.985,-17.97,-17.952,-17.94,-17.949,-17.975,-17.955,-17.923,-17.889,-17.798,-17.747,-17.706,-17.669,-17.675,-17.641,-17.556,-17.481,-17.399,-17.359,-17.337,-17.275,-17.295,-17.265,-17.222,-17.218,-17.241,-17.291,-17.301,-17.281,-17.23,-17.204,-17.17,-17.148,-17.15,-17.057,-17.024,-17.009,-16.852,-16.908,-16.969,-16.984,-16.958,-16.96,-16.957,-16.896,-16.935,-16.928,-16.942,-16.924,-16.898,-16.895,-16.841,-16.832,-16.843,-16.763,-16.741,-16.778,-16.734,-16.69,-16.698,-16.647,-16.654,-16.606,-16.551,-16.5,-16.488,-16.463,-16.472,-16.475,-16.449,-16.455,-16.467,-16.433,-16.414,-16.387,-16.378,-16.324,-16.268,-16.231,-16.205,-16.248,-16.188,-16.161,-16.129,-16.104,-16.052,-16.008,-15.991,-15.951,-15.943,-15.931,-15.928,-15.919,-15.884,-15.903,-15.882,-15.966,-15.916,-15.906,-15.907,-15.872,-15.864,-15.889,-15.877,-15.917,-15.945,-15.959,-15.976,-15.969,-15.98,-16.027,-15.994,-15.983,-16.001,-15.995,-16.151,-16.004,-16.014,-16.005,-15.985,-15.979,-16.06,-16.074,-16.105,-16.127,-16.219,-16.223,-16.227,-16.331,-16.32,-16.351,-16.357,-16.019,-16.397,-16.415,-16.28,-16.469,-16.479,-16.499,-16.539,-16.617,-16.649,-16.631,-16.732,-16.865,-16.9,-16.927,-17.013,-17.035,-17.077,-17.103,-17.129,-17.154,-17.164,-17.311,-17.456,-17.494,-17.525,-17.6,-17.581,-17.627,-17.668,-17.609,-17.819,-17.886,-17.845,-17.892,-17.891,-17.869,-17.913,-17.936,-17.931,-17.922,-18.1,-18.12,-18.122,-18.125,-18.132,-18.137,-18.173,-18.183,-18.218,-18.273,-18.307,-18.274,-18.323,-18.291,-18.294,-18.354,-18.517,-18.539,-18.559,-18.572,-18.608,-18.626,-18.661,-18.788,-18.841,-18.874,-18.872,-18.6,-18.561,-18.545,-18.577,-18.549,-18.469,-18.377,-18.372,-18.368,-18.28,-18.21,-18.116,-18.113,-18.156,-18.134,-18.203,-18.246,-18.301,-18.427,-18.493,-18.595,-18.66,-18.76,-18.889,-18.944,-18.957,-18.983,-19.116,-19.139,-19.151,-19.204,-19.263,-19.307,-19.359,-19.408,-19.447,-19.521,-19.543,-19.461,-19.491,-19.486,-19.462,-19.433,-19.481,-19.488,-19.473,-19.376,-19.383,-19.354,-19.442,-19.444,-19.453,-19.448,-19.475,-19.489,-19.5,-19.687,-19.746,-19.772,-19.843,-19.865,-19.844,-19.893,-19.935,-19.953,-20.034,-20.036,-20.102,-20.185,-20.23,-20.327,-20.364,-20.372,-20.476,-20.528,-20.585,-20.645,-20.693,-20.716,-20.805,-20.821,-20.872,-20.892,-20.922,-20.956,-20.984,-20.992,-21.044,-21.042,-21.071,-21.239,-21.29,-21.315,-21.406,-21.399,-21.554,-21.654,-21.73,-21.746,-21.785,-21.799,-21.882,-21.894,-21.972,-22.096,-22.108,-22.122,-22.203,-22.19,-22.26,-22.451,-22.472,-22.463,-22.448,-22.502,-22.495,-22.525,-22.501,-22.519,-22.561,-22.563,-22.591,-22.626,-22.629,-22.599,-22.589,-22.651,-22.649,-22.749,-22.82,-22.832,-22.814,-22.793,-22.773,-22.739,-22.668,-22.637,-22.635,-22.677,-22.679,-22.693,-22.67,-22.597,-22.548,-22.498,-22.459,-22.447,-22.434,-22.431,-22.396,-22.414,-22.397,-22.363,-22.33,-22.315,-22.299,-22.301,-22.296,-22.313)),row.names = c(NA,506L),class = "data.frame")

解决方法

首先,我们可以计算两次采样之间的时间间隔:

gap_days<-vector() #Create an empty vector we will fill in later
for (i in 1:dim(df)[1]){ #Iterate of each row in your dataframe
  if (i==1){ 
    gap_days<-c(gap_days,NA) #For the first observation there is no gap
  } else if (df$id[i] != df$id[i-1]){ 
    gap_days<-c(gap_days,NA) #For the first observation of a new ID there is no gap
  } else {
    gap_days<-c(gap_days,difftime(df$date[i],df$date[i-1],units = "days")[[1]]) #Calculate the time between observations in days
  }
}

df<-cbind(df,gap_days) #Bind our new column onto your data
Over1Day<-subset(df,gap_days > 1) #Save a dataframe of those observations of more than 1 day
Over1Day #print that df to the screen
      id                date lc     lon     lat gap_days
20162.03 2003-10-21 12:19:00  1 -38.517 -18.253 1.906944
20162.03 2003-10-23 10:33:00  B -38.674 -19.824 1.654861
20162.03 2003-11-02 08:14:00  B -42.084 -24.071 9.598611

我不确定需要将同一ID的音轨分为2个或更多子音轨,这意味着什么,有些人会有多于一个的爆发,但是我希望这可以帮助您开始。