0

test在 R 中有一个数据框 ()。其中一列包含此列表结构中的坐标:

> 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为其他形式,但通常会导致一些错误。如何将此列表转换为一行?

额外信息这是一个后续问题

使用 x,yx,y 坐标将数据框转换为 R 中的空间线数据框

按要求更新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"))
4

1 回答 1

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)

但请参阅此示例以获取更多信息。

当我试图将您的坐标强制转换为 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
于 2016-10-01T12:17:27.703 回答