Reputation: 281
I have a set of data that is a list of lists of vectors of coordinates that are separated into two groups
geom group
list(list(c(43, 43, 40, 40, 43, 10, 13, 13, 10, 10), list(... 1
list(c(95, 100, 100, 95, 95, -12, -12, -19, -19, -12) 2
I would like to convert this into a dataframe that looks like:
Longitude Latitude Group
43 10 1
43 13 1
etc.
To reproduce my data
structure(list(geom = structure(list(structure(list(list(structure(c(43,
43, 40, 40, 43, 10, 13, 13, 10, 10), .Dim = c(5L, 2L))), list(
structure(c(23, 23, 20, 20, 23, 10, 13, 13, 10, 10), .Dim = c(5L,
2L))), list(structure(c(25, 25, 38, 25, 10, 10.3, 10.3, 10
), .Dim = c(4L, 2L)))), class = c("XY", "MULTIPOLYGON", "sfg"
)), structure(list(structure(c(95, 100, 100, 95, 95, -12, -12,
-19, -19, -12), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg"))), n_empty = 0L, precision = 0, crs = structure(list(input = "EPSG:4326",
wkt = "GEOGCS[\"WGS 84\",\n DATUM[\"WGS_1984\",\n SPHEROID[\"WGS
84\",6378137,298.257223563,\n AUTHORITY[\"EPSG\",\"7030\"]],\n
AUTHORITY[\"EPSG\",\"6326\"]],\n PRIMEM[\"Greenwich\",0,\n
AUTHORITY[\"EPSG\",\"8901\"]],\n UNIT[\"degree\",0.0174532925199433,\n
AUTHORITY[\"EPSG\",\"9122\"]],\n AUTHORITY[\"EPSG\",\"4326\"]]"), class = "crs"), class =
c("sfc_GEOMETRY",
"sfc"), bbox = structure(c(xmin = 20, ymin = -19, xmax = 100,
ymax = 13), class = "bbox"), classes = c("MULTIPOLYGON", "POLYGON"
)), group = 1:2), row.names = 1:2, sf_column = "geom", agr = structure(c(group =
NA_integer_), class = "factor", .Label = c("constant",
"aggregate", "identity")), class = c("sf", "data.frame"))
Example of data split into more than two groups, in this case the data is split into three groups:
structure(list(geom =
structure(list(structure(list(list(structure(c(43,
43, 40, 40, 43, 10, 13, 13, 10, 10), .Dim = c(5L, 2L))), list(
structure(c(23, 23, 20, 20, 23, 10, 13, 13, 10, 10), .Dim = c(5L,
2L))), list(structure(c(25, 25, 38, 25, 10, 10.3, 10.3, 10
), .Dim = c(4L, 2L)))), class = c("XY", "MULTIPOLYGON", "sfg"
)), structure(list(structure(c(60, 60, 80, 80, 60, 6, 5.7, 5.7,
6, 6), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(95, 100, 100, 95, 95, -12, -12,
-19, -19, -12), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg"))), n_empty = 0L, precision = 0, crs = structure(list(
input = "EPSG:4326", wkt = "GEOGCS[\"WGS 84\",\n
DATUM[\"WGS_1984\",\n SPHEROID[\"WGS
84\",6378137,298.257223563,\n
AUTHORITY[\"EPSG\",\"7030\"]],\n
AUTHORITY[\"EPSG\",\"6326\"]],\n PRIMEM[\"Greenwich\",0,\n
AUTHORITY[\"EPSG\",\"8901\"]],\n
UNIT[\"degree\",0.0174532925199433,\n
AUTHORITY[\"EPSG\",\"9122\"]],\n AUTHORITY[\"EPSG\",\"4326\"]]"),
class = "crs"), class = c("sfc_GEOMETRY",
"sfc"), bbox = structure(c(xmin = 20, ymin = -19, xmax = 100,
ymax = 13), class = "bbox"), classes = c("MULTIPOLYGON", "POLYGON",
"POLYGON")), group = 1:3), row.names = c(NA, 3L), sf_column = "geom",
agr = structure(c(group = NA_integer_), class = "factor", .Label =
c("constant",
"aggregate", "identity")), class = c("sf", "data.frame"))
Upvotes: 0
Views: 62
Reputation: 39595
You can try this base R
solution, where List
is the dput()
you included in the question:
#Empty list
Mylist <- list()
#Loop
for(i in 1:dim(List)[1])
{
Mylist[[i]] <- do.call(rbind,lapply(List$geom[[i]],data.frame))
Mylist[[i]]$Group <- List$group[[i]]
}
#Now bind all
DF <- do.call(rbind,Mylist)
Output:
X1 X2 Group
1 43 10.0 1
2 43 13.0 1
3 40 13.0 1
4 40 10.0 1
5 43 10.0 1
6 23 10.0 1
7 23 13.0 1
8 20 13.0 1
9 20 10.0 1
10 23 10.0 1
11 25 10.0 1
12 25 10.3 1
13 38 10.3 1
14 25 10.0 1
15 95 -12.0 2
16 100 -12.0 2
17 100 -19.0 2
18 95 -19.0 2
19 95 -12.0 2
Names are standard based on your data, so you can rename as you want (X1
and X2
).
Also for more groups:
X1 X2 Group
1 43 10.0 1
2 43 13.0 1
3 40 13.0 1
4 40 10.0 1
5 43 10.0 1
6 23 10.0 1
7 23 13.0 1
8 20 13.0 1
9 20 10.0 1
10 23 10.0 1
11 25 10.0 1
12 25 10.3 1
13 38 10.3 1
14 25 10.0 1
15 60 6.0 2
16 60 5.7 2
17 80 5.7 2
18 80 6.0 2
19 60 6.0 2
20 95 -12.0 3
21 100 -12.0 3
22 100 -19.0 3
23 95 -19.0 3
24 95 -12.0 3
Upvotes: 1