0

I am using the sfnetworks package to simplify a road network in r that I can then use to do further analysis using dplyr and sf. After simplifying the road network I convert it back to an sf package using the st_as_sf() how I am getting this indexing at the start of my dataframe which shows the number of elements and is causing errors further along in my code. Does anyone know how to remove this. example of code below:

library(sfnetworks)
library(sf)
library(tidygraph)
library(igraph)
library(dbscan)

# create simple network
p1 = st_point(c(0, 1))
p2 = st_point(c(1, 1))
p3 = st_point(c(2, 1))
p4 = st_point(c(3, 1))
p5 = st_point(c(4, 1))
p6 = st_point(c(3, 2))
p7 = st_point(c(3, 0))
p8 = st_point(c(4, 3))
p9 = st_point(c(4, 2))
p10 = st_point(c(4, 0))
p11 = st_point(c(5, 2))
p12 = st_point(c(5, 0))
p13 = st_point(c(5, -1))
p14 = st_point(c(5.8, 1))
p15 = st_point(c(6, 1.2))
p16 = st_point(c(6.2, 1))
p17 = st_point(c(6, 0.8))
p18 = st_point(c(6, 2))
p19 = st_point(c(6, -1))
p20 = st_point(c(7, 1))

l1 = st_sfc(st_linestring(c(p1, p2, p3)))
l2 = st_sfc(st_linestring(c(p3, p4, p5)))
l3 = st_sfc(st_linestring(c(p6, p4, p7)))
l4 = st_sfc(st_linestring(c(p8, p11, p9)))
l5 = st_sfc(st_linestring(c(p9, p5, p10)))
l6 = st_sfc(st_linestring(c(p8, p9)))
l7 = st_sfc(st_linestring(c(p10, p12, p13, p10)))
l8 = st_sfc(st_linestring(c(p5, p14)))
l9 = st_sfc(st_linestring(c(p15, p14)))
l10 = st_sfc(st_linestring(c(p16, p15)))
l11 = st_sfc(st_linestring(c(p14, p17)))
l12 = st_sfc(st_linestring(c(p17, p16)))
l13 = st_sfc(st_linestring(c(p15, p18)))
l14 = st_sfc(st_linestring(c(p17, p19)))
l15 = st_sfc(st_linestring(c(p16, p20)))

lines = c(l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15)

# init sfnetwork
roads = as_sfnetwork(lines)
roads_simple = roads %>%
  activate("edges") %>%
  arrange(edge_length()) %>%
  filter(!edge_is_multiple()) %>%
  filter(!edge_is_loop())

roads_subdivision = convert(roads_simple, to_spatial_subdivision)
roads_smoothed = convert(roads_subdivision, to_spatial_smooth)

# convert back to sf
roads_subdivision.sf <- roads_subdivision %>% 
  activate("edges") %>% 
  st_as_sf()

I want to end up with a dataframe that doesn't have that list at the start like this: enter image description here

  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please [do not post code or data in images](https://meta.stackoverflow.com/q/285551/2372064) – MrFlick May 10 '23 at 19:48
  • Sorry about that, just included the simplest example I could think of – Alex Tabascio May 10 '23 at 20:04
  • Is this about removing columns that were added by sfnetworok / tidygraph? In that case it might not be the best example as by creating a network from linestrings, there is no attribute table and you'd just need geometry column from network edges. sfnetworks comes with `sfnetworks::roxel` dataset, maybe use that in your example? Though it seems that you are asking for `roads_subdivision.sf %>% select(names(roads))` or just `roads_subdivision.sf[,names(roads)]` – margusl May 11 '23 at 07:47
  • Moreover, if you want to remove the column named `.tidygraph_node_index`, you need to add `.clean = TRUE` when calling `convert()`. See also `?convert`. – agila May 11 '23 at 07:53
  • @margusl ya I was looking for what you were referring to in the last line of your comment. Still unsure why that is coming up – Alex Tabascio May 11 '23 at 14:10

0 Answers0