Converts an sf object to a data.frame
sf_to_df(sf, fill = FALSE, unlist = NULL)sf object
logical indicating if the resulting data.frame should be filled
with the data columns from the sf object. If TRUE, each row of data will
be replicated for every coordinate in every geometry.
string vector of columns to unlist. Each list element is equivalent to a row of the input object, and is expected to be the same length as the number of coordinates in the geometry.
df <- data.frame(
ml_id = c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2)
, l_id = c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2)
, x = rnorm(15)
, y = rnorm(15)
, z = rnorm(15)
, m = rnorm(15)
)
sf <- sf_polygon( obj = df, polygon_id = "ml_id", linestring_id = "l_id" )
df <- sf_to_df( sf )
## with associated data
sf$val1 <- c("a","b")
sf$val2 <- c(1L, 2L)
df <- sf_to_df( sf, fill = TRUE )
## Unlisting list columns
df <- data.frame(
l_id = c(1,1,1,2,2,2,3,3,3,3)
, x = rnorm(10)
, y = rnorm(10)
)
sf <- sf_linestring( obj = df, linestring_id = "l_id" , x = "x", y = "y")
## put on a list column
sf$l <- list( c(1,2,3),c(3,2,1),c(10,11,12,13))
sf_to_df( sf, unlist = "l" )
#> sfg_id linestring_id x y l
#> 1 1 1 -0.2778506 0.15910490 1
#> 2 1 1 -0.1279812 0.92932855 2
#> 3 1 1 1.5191177 -0.99309377 3
#> 4 2 2 -0.5227884 0.09142845 3
#> 5 2 2 0.8296614 -1.83318044 2
#> 6 2 2 0.8778343 -0.41390456 1
#> 7 3 3 0.8499457 -0.80153601 10
#> 8 3 3 0.4246130 -0.69565300 11
#> 9 3 3 -1.0167938 -1.80572796 12
#> 10 3 3 1.4491113 -0.88293511 13