2016-09-30 27 views
0

我有一個數據幀(test)的列表中的一列包含該列表中的結構座標:在數據幀轉換爲在河裏面一個線

> dput(test$coordinates) 
list(structure(list(x = c(-1.294832, -1.294883, -1.294262, 
-1.249478), y = c(54.61024, 54.61008, 54.610016, 54.610006 
)), .Names = c("x", "y"), row.names = c(NA, -284L), class = c("tbl_df", 
"tbl", "data.frame"))) 

我已經減少座標的數量明晰。

最終我希望將數據幀轉換爲空行數據幀,但要做到這一點,我需要test$coordinates行形式。不過,我得到以下錯誤

> lines(test$coordinates) 
Error in xy.coords(x, y) : 
    'x' is a list, but does not have components 'x' and 'y' 

我試圖給test$coordinates轉換爲其他形式,但它通常會導致一些錯誤。如何將這個列表轉換爲一行?

額外的信息,這是一個後續問題

Convert data frame to spatial lines data frame in R with x,y x,y coordintates

更新的要求dput(head(test))

> dput(head(test)) 
structure(list(rid = 1, start_id = 1L, start_code = "E02002536", 
    end_id = 106L, end_code = "E02006909", strategy = "fastest", 
    distance = 12655L, time_seconds = 2921L, calories = 211L, 
    document.id = 1L, array.index = 1L, start = "Geranium Close", 
    finish = "Hylton Road", startBearing = 0, startSpeed = 0, 
    start_longitude = -1.294832, start_latitude = 54.610241, 
    finish_longitude = -1.249478, finish_latitude = 54.680691, 
    crow_fly_distance = 8362, event = "depart", whence = 1473171787, 
    speed = 20, itinerary = 419956, clientRouteId = 0, plan = "fastest", 
    note = "", length = 12655, time = 2921, busynance = 42172, 
    quietness = 30, signalledJunctions = 3, signalledCrossings = 2, 
    west = -1.300074, south = 54.610006, east = -1.232447, north = 54.683814, 
    name = "Geranium Close to Hylton Road", walk = 0, leaving = "2016-09-06 15:23:07", 
    arriving = "2016-09-06 16:11:48", grammesCO2saved = 2359, 
    calories2 = 211, type = "route", coordinates = list(structure(list(
     x = c(-1.294832, -1.294883, -1.294262, -1.294141, -1.29371, 
     -1.293726, -1.293742, -1.29351, -1.293368, -1.292816, 
     -1.248019, -1.249478), y = c(54.61024, 54.61008, 54.610016, 
     54.610006, 54.610038, 54.610142, 54.610247, 54.610262, 
     54.681238, 54.680975, 54.680601, 54.680404 
     )), .Names = c("x", "y"), row.names = c(NA, -284L), class = c("tbl_df", 
    "tbl", "data.frame")))), .Names = c("rid", "start_id", "start_code", 
"end_id", "end_code", "strategy", "distance", "time_seconds", 
"calories", "document.id", "array.index", "start", "finish", 
"startBearing", "startSpeed", "start_longitude", "start_latitude", 
"finish_longitude", "finish_latitude", "crow_fly_distance", "event", 
"whence", "speed", "itinerary", "clientRouteId", "plan", "note", 
"length", "time", "busynance", "quietness", "signalledJunctions", 
"signalledCrossings", "west", "south", "east", "north", "name", 
"walk", "leaving", "arriving", "grammesCO2saved", "calories2", 
"type", "coordinates"), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame")) 
+0

也許最好包含'dput(head(test))'的輸出。話雖如此,也許'tidyr'軟件包中的'unnest'可以提供幫助。 – Jaap

+0

@zheyuan Li,no。這個問題旨在創造空間線。 – loki

+0

@ProcrastinatusMaximus我已經將問題添加到了問題 – falcs

回答

0

lines是一個繪圖功能。我假設你想要sp::SpatialLines。有關如何構建這樣的對象,請參見?"SpatialLines-class"

這裏是你的情況,只要你沒有「損壞」data.frame(見本文的底部)。

library(sp) 
coords <- as.data.frame(xy$coordinates[[1]])[1:12, ] 

out <- SpatialLines(list(Lines(list(Line(coords)), ID = 1))) 

An object of class "SpatialLines" 
Slot "lines": 
[[1]] 
An object of class "Lines" 
Slot "Lines": 
[[1]] 
An object of class "Line" 
Slot "coords": 
      x  y 
1 -1.294832 54.61024 
2 -1.294883 54.61008 
3 -1.294262 54.61002 
4 -1.294141 54.61001 
5 -1.293710 54.61004 
6 -1.293726 54.61014 
7 -1.293742 54.61025 
8 -1.293510 54.61026 
9 -1.293368 54.68124 
10 -1.292816 54.68097 
11 -1.248019 54.68060 
12 -1.249478 54.68040 



Slot "ID": 
[1] "1" 



Slot "bbox": 
     min  max 
x -1.294883 -1.248019 
y 54.610006 54.681238 

Slot "proj4string": 
CRS arguments: NA 

將數據添加到這個對象,你應該使用

SpatialLinesDataFrame(out, data = yourdata) 

但見this example獲取更多信息。

當我試圖將座標強制爲data.frame時會出現警告。希望這不是你的數據集的情況。

> as.data.frame(xy$coordinates[[1]]) 
      x  y 
1 -1.294832 54.61024 
2 -1.294883 54.61008 
3 -1.294262 54.61002 
... 
281  <NA>  <NA> 
282  <NA>  <NA> 
283  <NA>  <NA> 
284  <NA>  <NA> 

Warning message: 
In format.data.frame(x, digits = digits, na.encode = FALSE) : 
    corrupt data frame: columns will be truncated or padded with NAs