如何将向量列表的列表转换为数据框
How to convert list of lists of vectors to a dataframe
我有一组数据,它是分成两组的坐标向量列表的列表
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
我想将其转换成如下所示的数据框:
Longitude Latitude Group
43 10 1
43 13 1
等等
重现我的数据
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"))
数据分成两组以上的示例,在本例中数据分为三组:
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"))
您可以试试这个 base R
解决方案,其中 List
是您在问题中包含的 dput()
:
#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)
输出:
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
名称是基于您的数据的标准名称,因此您可以根据需要重命名(X1
和 X2
)。
还有更多群组:
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
我有一组数据,它是分成两组的坐标向量列表的列表
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
我想将其转换成如下所示的数据框:
Longitude Latitude Group
43 10 1
43 13 1
等等
重现我的数据
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"))
数据分成两组以上的示例,在本例中数据分为三组:
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"))
您可以试试这个 base R
解决方案,其中 List
是您在问题中包含的 dput()
:
#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)
输出:
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
名称是基于您的数据的标准名称,因此您可以根据需要重命名(X1
和 X2
)。
还有更多群组:
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