2017-03-06 56 views
0

我正在做一個動物跟蹤項目。我的數據「finaltrimmed」看起來像這樣從點座標創建SpatialLinesDataFrame

TrackIndex  Time x_position y_position 
1    1 0.1034  425  171 
2    1 0.1379  425  169 
3    1 0.1724  427  166 
......... 
125   25 1.1030  462  397 
126   25 1.1380  462  397 
127   25 1.1720  462  397 
128   25 1.2070  462  397 
129   25 1.2410  461  398 
130   25 1.2760  462  399 
131   25 1.3100  461  399 
132   25 1.3450  461  399 
133   25 1.3790  460  399 
134   25 1.4140  460  399 
..... 
268   41 1.8280  302  280 
269   41 1.8620  303  279 
270   41 1.8970  302  280 
271   41 1.9310  302  280 
272   41 1.9660  302  281 
273   41 2.0000  302  281 
274   41 2.0340  302  281 
275   41 2.0690  302  282 
276   41 2.1030  302  282 
277   41 2.1380  302  282 
278   41 2.1720  302  283 
........ 

我要爲每一個獨特的TrackIndex,隨着時間的推移,基本上如何跟蹤每個個體昆蟲招行。從那裏我想創建一個基於TrackIndex的SpatialLinesDataFrame。最後,我想在「adehabitatMA」包中使用「緩衝區」功能在每條線周圍創建一個緩衝區。

我能創建使用以下命令SpatialPointsDataFrame。

xy<-cbind(finaltrimmed$x_position,finaltrimmed$y_position) 
MatrixofPoints<-matrix(xy,ncol=2) 
points<-SpatialPoints(MatrixofPoints) 
dataframe=data.frame(finaltrimmed$TrackIndex) 
df.points<-SpatialPointsDataFrame(points,dataframe) 

但是,我無法以類似的方式創建SpatialLinesDataFrame。

我的想法是分割數據幀「最終修整」先用「分裂」功能。

splitfinal<-split(finaltrimmed,finaltrimmed$TrackIndex) 

這使我有下面的數據結構 $ 1 TrackIndex時間x_position y_position newindex 1:1246 347.0 316 214 1 2:1246 347.0 316 214 2 ...... :1246 348.9 325 201 57 58 :1246 349.0 330 201 58 TrackIndex時間x_position y_position newindex

$ 25 TrackIndex時間x_position y_position newindex 1:1318 363.6 375 422 1 2:1318 363.7 375 422 2 ..... 57:1318 365.6 399 406 57 58:1318 365.6 400 406 58 從那裏,我可以cbind的x和y位置在「splitfinal」(這個步驟沒有解決,因爲「splitfinal」是列表的列表)。我也不確定如何創建一個Lines類,這是創建SpatialLinesDataFrame所必需的。

我一直堅持了很多天,想不出辦法。

任何人都可以幫忙嗎?

+0

沒有使用它自己,但是這可能工作:https://開頭rpubs.com/walkerke/points_to_line – lbusett

回答

0

下面是應該工作的方法:

示例數據:

finaltrimmed <- read.table(text="TrackIndex Time x_position y_position 
1 1 0.1034 425 171 
2 1 0.1379 425 169 
3 1 0.1724 427 166 
130 25 1.2760 462 399 
131 25 1.3100 461 399 
132 25 1.3450 461 399 
133 25 1.3790 460 399 
134 25 1.4140 460 399 
274 41 2.0340 302 281 
275 41 2.0690 302 282 
276 41 2.1030 302 282 
277 41 2.1380 302 282 
278 41 2.1720 302 283") 

解決方案:

library(raster) 
ft <- split(finaltrimmed, finaltrimmed$TrackIndex) 

z <- lapply(ft, function(i) spLines(as.matrix(i[, c('x_position', 'y_position')]), attr=data.frame(TrackIndex=i$TrackIndex[1]))) 
names(z) <- NULL 
zz <- do.call(bind, z)