dcooley / sfheaders

Build sf objects from R and Rcpp
https://dcooley.github.io/sfheaders/
Other
74 stars 5 forks source link

to_df - List columns #69

Closed dcooley closed 4 years ago

dcooley commented 4 years ago

Currently list-columns are treated like vectors - each 'row' is expanded

library(sfheaders)

df <- data.frame(
  id = c(1,1,1,2,2)
  , x = 1:5
  , y = 1:5
)

sf <- sf_linestring(obj = df, linestring_id = "id")
sf$l <- list(c(1,2,3),c(4,5))
sf

#   id         geometry       l
# 1  1 1, 2, 3, 1, 2, 3 1, 2, 3
# 2  2       4, 5, 4, 5    4, 5

sf_to_df( sf, fill = T )
#   id       l sfg_id linestring_id x y
# 1  1 1, 2, 3      1             1 1 1
# 2  1 1, 2, 3      1             1 2 2
# 3  1 1, 2, 3      1             1 3 3
# 4  2    4, 5      2             2 4 4
# 5  2    4, 5      2             2 5 5

But it may be necessary, especially for mapdeck, to have a list-column of values which apply to each coordinate, like a colouring value. In which case they should be un-listed like the coordinates, to give

df$l <- c(1,2,3,4,5)
df
# df
#   id l sfg_id linestring_id x y
# 1  1 1      1             1 1 1
# 2  1 2      1             1 2 2
# 3  1 3      1             1 3 3
# 4  2 4      2             2 4 4
# 5  2 5      2             2 5 5

I think the easiest way to make this work is to have an unlist = c() argument, where you specify the columns you want to unlist. The un-listing is then done at the last step in sf_to_df() (in C++)

TODO