I'm having some trouble with rowwise operations on sf objects, where the geometry is polygon type.
sf functions inside by_row don't seem to work e.g. the following should create a list-column holding bounding box objects:
purrr::by_row(sf_polygons_object, function(x) {
list(st_bbox(x))
}, .to = 'bb')
Error in UseMethod("st_bbox") : no applicable method for 'st_bbox' applied to an object of class "c('tbl_df', 'data.frame')"
(not the most useful example on its own, but demonstrates the problem).
I've tried a few alternative approaches e.g. rowwise() %>% mutate()
, mutate( x = apply(., 1, function(x) ...))
, and none work as they don't supply st_bbox() with the sf object it requires. Is this a bug, or am I approaching the problem poorly?
edit: reproducible example
library(sp)
library(rgdal)
library(rgeos)
library(sf)
library(tidyverse)
library(rnaturalearth)
nec <- st_as_sf(ne_countries()[1:5,]) %>%
purrr::by_row(., function(x) st_bbox(x), .to = 'bb')