通过课程ltraj循环?

时间:2016-03-01 20:56:23

标签: r

道歉,如果这不是查询此问题的最佳论坛。

有没有人能够循环/迭代虽然在ltraj类(adehabitatlt)中有多个GPS抓住了个人?

我一直在尝试为多个人计算Prox(https://cran.r-project.org/web/packages/wildlifeDI/vignettes/wildlifeDI-vignette.pdf),但我正在努力完成如何循环ltraj类,因为它与数据帧(我习以为常)不同。

提前致谢。

install.packages('wildlifeDI', dependencies=TRUE)
library(wildlifeDI)
library(adehabitatLT)
chupacabra <- structure(list(CollarID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L), .Label = c("A4116F", "A4117M", "A4118F"), class =       "factor"), 
DateTime = structure(c(1433653200, 1433667600, 1433682060, 
1433682300, 1433682600, 1433682900, 1433683200, 1433683500, 
1433683800, 1433684100, 1433684400, 1433684700, 1433685000, 
1433685300, 1433685600, 1433685900, 1433686200, 1433686500, 
1433686800, 1433687100, 1433687400, 1433687700, 1433688000, 
1433688300, 1433688600, 1433688900, 1433689200, 1433689500, 
1433689800, 1433690100, 1433690400, 1433690700, 1433691000, 
1433691300, 1433691600, 1433691900, 1433692200, 1433692500, 
1433692800, 1433693100, 1433693400, 1433693700, 1433694000, 
1433694300, 1433694600, 1433694900, 1433695200, 1433695500, 
1433695800, 1433696100, 1433696400, 1433710860, 1433714400, 
1433714700, 1433715000, 1433715300, 1433715600, 1433715900, 
1433716200, 1433716500, 1433716800, 1433717100, 1433717400, 
1433717700, 1433718000, 1433718300, 1433718600, 1433718900, 
1433719200, 1433719500, 1433719800, 1433720100, 1433720400, 
1433720700, 1433721000, 1433721300, 1433721600, 1433721900, 
1433722200, 1433722500, 1433722800, 1433723100, 1433723400, 
1433723700, 1433724060, 1433724300, 1433724600, 1433724900, 
1433725200, 1433653200, 1433667660, 1433682060, 1433682300, 
1433682600, 1433682900, 1433683200, 1433683500, 1433683800, 
1433684100, 1433684400, 1433684700, 1433685000, 1433685300, 
1433685660, 1433685900, 1433686200, 1433686500, 1433686800, 
1433687100, 1433687400, 1433687700, 1433688000, 1433688300, 
1433688660, 1433688900, 1433689200, 1433689500, 1433689800, 
1433690100, 1433690400, 1433690700, 1433691000, 1433691300, 
1433691600, 1433691900, 1433692200, 1433692500, 1433692800, 
1433693100, 1433693400, 1433693700, 1433694060, 1433694300, 
1433694600, 1433694900, 1433695200, 1433695500, 1433695800, 
1433696100, 1433696400, 1433710860, 1433714400, 1433714700, 
1433715000, 1433715300, 1433715600, 1433715900, 1433716200, 
1433716500, 1433716800, 1433717100, 1433717400, 1433717700, 
1433718000, 1433718300, 1433718600, 1433718900, 1433719200, 
1433719500, 1433719800, 1433720100, 1433720400, 1433720700, 
1433721000, 1433721300, 1433721600, 1433721900, 1433722200, 
1433722500, 1433722800, 1433723100, 1433723400, 1433723700, 
1433724000, 1433724300, 1433724600, 1433724900, 1433725200, 
1433653249, 1433667666, 1433682089, 1433682349, 1433682632, 
1433682936, 1433683234, 1433683536, 1433683837, 1433684144, 
1433684443, 1433684739, 1433685031, 1433685370, 1433685634, 
1433685935, 1433686236, 1433686536, 1433686826, 1433687142, 
1433687448, 1433687736, 1433688034, 1433688337, 1433688649, 
1433688936, 1433689236, 1433689531, 1433689827, 1433690139, 
1433690433, 1433690736, 1433691048, 1433691336, 1433691634, 
1433691941, 1433692236, 1433692535, 1433692833, 1433693129, 
1433693434, 1433693735, 1433694028, 1433694373, 1433694642, 
1433694931, 1433695234, 1433695542, 1433695831, 1433696148, 
1433696448, 1433710908, 1433714437, 1433714737, 1433715036, 
1433715366, 1433715636, 1433715969, 1433716234, 1433716536, 
1433716827, 1433717137, 1433717435, 1433717733, 1433718048, 
1433718336, 1433718636, 1433718973, 1433719272, 1433719530, 
1433719837, 1433720136, 1433720431, 1433720736, 1433721031, 
1433721336, 1433721640, 1433721946, 1433722236, 1433722528, 
1433722842, 1433723137, 1433723434, 1433723730, 1433724035, 
1433724370, 1433724634, 1433724936, 1433725236), class = c("POSIXct", 
"POSIXt"), tzone = ""), UTM_X = c(636979.2503, 636977.6583, 
637402.4471, 637400.3063, 637402.3105, 637407.1977, 637406.3305, 
637408.2991, 637407.1907, 637407.8414, 637406.7617, 637407.1614, 
637409.8019, 637431.5235, 637465.9644, 637495.9583, 637525.2219, 
637573.6033, 637645.3501, 637683.3844, 637691.6229, 637693.4815, 
637693.4973, 637691.2483, 637691.9061, 637693.6377, 637692.1106, 
637692.3169, 637690.9989, 637691.4503, 637693.6252, 637692.4915, 
637694.9434, 637692.6685, 637692.8116, 637694.6787, 637694.4404, 
637695.9109, 637696.8945, 637695.2403, 637695.4283, 637694.6085, 
637693.4962, 637695.6229, 637734.7283, 637773.2897, 637774.9891, 
637787.6573, 637792.285, 637807.0486, 637834.6231, 637497.3348, 
637149.9982, 637145.0345, 637178.159, 637181.8251, 637181.1075, 
637178.023, 637175.327, 637179.9138, 637180.2833, 637181.5512, 
637185.8749, 637181.0011, 637177.401, 637177.4498, 637176.787, 
637176.0093, 637175.5126, 637177.9578, 637178.5819, 637188.3911, 
637188.7303, 637189.496, 637204.3885, 637195.2063, 637204.9823, 
637201.5235, 637212.3355, 637274.4294, 637293.0009, 637296.3954, 
637331.3382, 637358.4369, 637365.1677, 637357.5562, 637355.3896, 
637345.4827, 637339.1054, 628920.3789, 628869.9781, 630028.6781, 
630156.4557, 629878.756, 629658.9786, 629412.6432, 629257.5965, 
629405.8967, 629113.4479, 628955.5124, 628852.0231, 628711.9202, 
628632.7134, 628621.7724, 628622.2565, 628683.6018, 628771.1182, 
628790.8437, 628867.7592, 628881.9794, 628830.9898, 628681.9202, 
628575.3395, 628578.1836, 628656.4902, 628659.2271, 628656.689, 
628660.4677, 628657.294, 628657.077, 628689.6585, 628727.0131, 
628716.6979, 628703.8397, 628678.6953, 628679.3594, 628681.3549, 
628625.6275, 628563.1372, 628488.425, 628482.5023, 628469.2209, 
628417.9697, 628407.7352, 628405.374, 628393.143, 628394.0092, 
628396.2344, 628395.05, 628395.7787, 627684.7989, 627704.889, 
627702.5528, 627702.0422, 627708.7906, 627706.9374, 627687.0371, 
627622.0573, 627605.7932, 627603.5707, 627587.8803, 627606.0471, 
627603.2967, 627602.954, 627603.5844, 627604.1232, 627601.697, 
627581.6104, 627599.7062, 627616.327, 627661.7402, 627889.446, 
627883.5896, 627803.1167, 627792.5918, 627716.0886, 627720.8854, 
627671.8217, 627666.9994, 627586.7035, 627584.4273, 627532.492, 
627502.6326, 627430.6781, 627408.8845, 627357.5049, 627406.0466, 
627427.1382, 636666.3215, 636629.7032, 637179.9041, 637187.7067, 
637183.5281, 637193.2082, 637227.2331, 637290.2543, 637347.9311, 
637373.0887, 637368.8923, 637371.0722, 637383.95, 637480.1799, 
637510.543, 637558.428, 637676.2714, 637682.3564, 637680.8591, 
637682.8516, 637680.8317, 637680.8341, 637681.9818, 637681.2897, 
637681.3658, 637681.9234, 637681.8824, 637682.0629, 637684.8756, 
637681.602, 637682.7548, 637680.8578, 637682.9887, 637680.2496, 
637681.4629, 637682.3731, 637682.2223, 637684.1076, 637681.7127, 
637681.1249, 637681.6758, 637681.595, 637682.5253, 637702.3094, 
637728.9487, 637784.0853, 637776.5727, 637785.2538, 637786.6413, 
637807.9935, 637834.8672, 637485.5191, 637148.5674, 637139.2974, 
637174.9104, 637191.9371, 637179.4262, 637175.7715, 637176.3455, 
637174.5459, 637174.2012, 637173.7462, 637177.3967, 637176.6907, 
637177.8458, 637178.0774, 637178.4151, 637178.3272, 637178.2442, 
637177.6655, 637176.734, 637186.2713, 637185.0998, 637197.4201, 
637197.9147, 637204.1485, 637203.1784, 637204.4993, 637205.3515, 
637279.9058, 637303.773, 637303.5724, 637330.3473, 637354.416, 
637366.5627, 637340.7274, 637357.5505, 637350.709, 637349.689
), UTM_Y = c(3365828.581, 3365826.066, 3364992.673, 3364991.006, 
3364989.036, 3364990.816, 3364989.486, 3364991.849, 3364991.37, 
3364990.059, 3364989.58, 3364991.403, 3364991.536, 3364985.614, 
3365030.733, 3365054.446, 3365091.064, 3365138.444, 3365289.033, 
3365390.111, 3365398.839, 3365387.124, 3365390.427, 3365386.696, 
3365387.104, 3365379.344, 3365386.131, 3365388.805, 3365385.152, 
3365385.158, 3365386.394, 3365385.637, 3365385.48, 3365386.071, 
3365385.397, 3365387.416, 3365387.269, 3365389.505, 3365389.971, 
3365387.833, 3365389.676, 3365390.685, 3365385.96, 3365384.934, 
3365352.152, 3365369.878, 3365376.795, 3365390.013, 3365382.689, 
3365382.189, 3365410.939, 3365683.847, 3365620.829, 3365574.121, 
3365527.084, 3365501.513, 3365502.801, 3365512.739, 3365514.733, 
3365512.885, 3365511.016, 3365512.562, 3365510.255, 3365511.235, 
3365509.494, 3365509.439, 3365509.431, 3365509.388, 3365510.678, 
3365509.534, 3365511.083, 3365511.85, 3365514.659, 3365513.371, 
3365525.476, 3365526.036, 3365529.429, 3365528.676, 3365513.172, 
3365507.793, 3365514.623, 3365512.105, 3365504.477, 3365512.401, 
3365495.238, 3365490.863, 3365441.075, 3365411.542, 3365403.003, 
3371496.516, 3371594.382, 3370587.966, 3370380.241, 3370270.012, 
3370346.817, 3370433.295, 3370488.189, 3370225.222, 3370122.896, 
3370174.202, 3370232.298, 3370192.371, 3370255.722, 3370283.548, 
3370283.21, 3370305.674, 3370344.002, 3370354.4, 3370348.973, 
3370200.353, 3370078.071, 3370123.589, 3370194.686, 3370393.878, 
3370500.265, 3370498.635, 3370498.882, 3370497.663, 3370499.687, 
3370500.172, 3370633.763, 3370704.904, 3370839.426, 3370879.943, 
3370950.842, 3370957.988, 3370963, 3371031.496, 3371082.487, 
3371109.89, 3371112.17, 3371118.807, 3371167.072, 3371168.581, 
3371170.127, 3371178.074, 3371177.097, 3371178.11, 3371176.777, 
3371178.482, 3371566.662, 3371622.632, 3371621.252, 3371619.772, 
3371623.975, 3371627.245, 3371636.71, 3371612.734, 3371598.776, 
3371590.192, 3371636.009, 3371656.352, 3371656.719, 3371656.471, 
3371656.755, 3371659.1, 3371656.401, 3371688.243, 3371717.065, 
3371741.492, 3371755.505, 3371618.156, 3371595.308, 3371615.82, 
3371560.55, 3371552.166, 3371572.884, 3371547.544, 3371530.616, 
3371559.755, 3371591.63, 3371612.877, 3371657.663, 3371727.149, 
3371739.263, 3371823.645, 3371912.149, 3371969.549, 3366104.602, 
3365712.344, 3365494.627, 3365496.045, 3365484.575, 3365475.02, 
3365485.304, 3365467.377, 3365477.805, 3365507.809, 3365510.682, 
3365519.888, 3365527.19, 3365491.394, 3365490.37, 3365468.274, 
3365393.413, 3365389.355, 3365386.964, 3365391.977, 3365389.125, 
3365388.937, 3365389.35, 3365389.375, 3365387.159, 3365387.133, 
3365386.578, 3365386.735, 3365386.161, 3365387.472, 3365387.487, 
3365387.064, 3365387.977, 3365385.016, 3365387.836, 3365388.036, 
3365387.048, 3365389.909, 3365387.074, 3365384.939, 3365387.717, 
3365388.026, 3365388.16, 3365385.728, 3365344.996, 3365374.693, 
3365377.679, 3365387.866, 3365389.823, 3365391.779, 3365410.631, 
3365698.174, 3365622.297, 3365571.954, 3365511.957, 3365510.265, 
3365505.086, 3365509.196, 3365512.44, 3365513.438, 3365508.777, 
3365509.049, 3365509.838, 3365506.403, 3365507.748, 3365510.711, 
3365509.075, 3365507.666, 3365508.152, 3365505.285, 3365498.401, 
3365508.531, 3365508.483, 3365513.538, 3365520.783, 3365519.376, 
3365523.92, 3365529.634, 3365529.866, 3365498.661, 3365512.941, 
3365509.801, 3365503.056, 3365513.548, 3365502.683, 3365482.215, 
3365438.852, 3365412.317, 3365406.363)), .Names = c("CollarID", 
"DateTime", "UTM_X", "UTM_Y"), row.names = c(NA, -267L), class = "data.frame")

评论错误的转化

# chupacabra$DateTime <-as.POSIXct(strptime(chupacabra$DateTime, format='%m/%d/%Y %H:%M:%S'),origin='1970-01-01') 

chupacabra2<-as.ltraj(chupacabra[, c("UTM_X","UTM_Y")], date=chupacabra$DateTime,id=chupacabra$CollarID, typeII=TRUE)

monster1<-chupacabra2[1] #extract the first chupacabra

monster2<-chupacabra2[2] #extract the second chupacabra

proxdf <-Prox(monster1,monster2, tc=0.5*60,dc=210, local =TRUE)

以下是我们一直在跟踪的chupcabras示例。我们想检查一下他们互相交流的频率。这个数据集有3个人(但是我们有许多更多的chupcabras)并且将1/1的动物/生物拉出来计算接近度是低效的。我想做一个for循环(也许是我的唯一ID),但是当数据是ltraj格式时我不明白怎么做。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

删除错误重新格式化DateTime值的有害代码后,我绘制了3个2动物组合的“轨迹”。显然,动物1不与动物2相互作用,因为它们的范围是不相交的。动物1和动物3确实相互作用,因为它们的关节寄生虫大约在其中间具有大致相同的轨迹:

 plot( chupacabra2[[1]]$x, chupacabra2[[1]]$y, type="l", 
       xlim=range( c(chupacabra2[[1]]$x, chupacabra2[[3]]$x)), 
       ylim= range(c(chupacabra2[[1]]$y, chupacabra2[[3]]$y)))
 lines( chupacabra2[[3]]$x, chupacabra2[[3]]$y, col="red")

enter image description here

这在proxdf13中显示为:

> monster1<-chupacabra2[1] 
> 
> monster3<-chupacabra2[3] 
> 
> proxdf13 <-Prox(monster1,monster3, tc=0.5*60,dc=210, local =TRUE)
> proxdf13
                  date       prox
1  2015-06-07 06:01:00 549.074863
2  2015-06-07 07:20:00 104.169909
3  2015-06-07 08:10:00   6.205875
4  2015-06-07 09:05:00  14.409016
5  2015-06-07 09:20:00  11.189309
6  2015-06-07 15:40:00   6.481131
7  2015-06-07 16:25:00   4.259042
8  2015-06-07 17:15:00  10.648210
9  2015-06-07 17:35:00   4.181297
10 2015-06-07 17:41:00   7.574566

所以猜测“相互作用”(可能)意味着“在15个距离单位内连续超过2天”的内容。因此,要考虑的自然功能是rle

> rle( proxdf13$prox < 15 )
Run Length Encoding
  lengths: int [1:2] 2 8
  values : logi [1:2] FALSE TRUE
> RL13 <- rle( proxdf13$prox < 15 )
> max( RL13$lengths [ RL13$values] )
[1] 8

并测试这是否大于某个值,比如2?

这就是处理单一动物 - 动物组合的方法。下面提问者的评论表明我可能在随后的材料中失去了他。要获得序列的2路组合,请使用combn

> combn(1:3, 2)
     [,1] [,2] [,3]
[1,]    1    1    2
[2,]    2    3    3

这是一个矩阵,用于生成索引值,用于分别使用每列从'ltraj'对象一次拉动两个单一动物“轨迹”数据帧。当使用apply函数时,它可以“循环”在矩阵的etiehr行或列上,并使用2作为指定列的apply的索引值。

所以把这一切放在一起(使用apply循环遍历combn的列索引 - 得到双向组合):

( max.days.prox <-  apply( combn( seq(length(chupacabra2)), 2 ), 2, 
                           # loops over columns of the "combinations matrix"
                          function(x) { 
                              proxcomb <- Prox( chupacabra2[ x[1] ], chupacabra2[ x[2] ],
                                                 tc=0.5*60,dc=210, local =TRUE)
                              RLcomb <- rle( proxcomb$prox < 15 )
                              Interact.days <- max( RLcomb$lengths [ RLcomb$values] ) }  ) )
# [1] -Inf    8 -Inf

我们在结果中查看感兴趣的项目:

> rbind(combn( seq(length(chupacabra2)), 2 ) , max.days.prox)
              [,1] [,2] [,3]
                 1    1    2
                 2    3    3
max.days.prox -Inf    8 -Inf

因此,只有动物1和动物3的配对提供了相互作用的证据。这将推广到ltraj - 对象

的更大实例