1

I am trying split a SpatialLinesDataFrame by attributes I merged into @data

I extracted a list of line ID's associated with a points data frame and aggregated them by category and line ID in dplyr

# A tibble: 1,430 x 3
# Groups:   Crime type [14]
   `Crime type`          nearest_line_id                          n
   <chr>                 <fct>                                <int>
 1 Anti-social behaviour 00206205-7878-4F17-BFF3-6086683F041B     1
 2 Anti-social behaviour 00937739-39C7-47E6-888A-4890FA2D47D8     1
 3 Anti-social behaviour 0108B4DD-049A-4A1D-A7CD-8F216AA0DB06     1
 4 Anti-social behaviour 018FB4F0-2DB1-48C4-A627-52A358B99E34     1
 5 Anti-social behaviour 025D1F38-C552-4D84-AC0D-6A1178C10B0F     1
 6 Anti-social behaviour 04542A56-3E24-4F25-AA64-CA874E7E3B5E     1
 7 Anti-social behaviour 04ACF5BC-78A0-475A-9294-F878E53A8625     1
 8 Anti-social behaviour 04AE68AE-E0A9-42B2-908E-29F4EC92051D     1
 9 Anti-social behaviour 05068729-E1D9-4AC6-86BA-06B126395516     1
10 Anti-social behaviour 06E16FFC-F03D-46D3-89A2-E20DDB19DA1C     1

I then merged this dataframe into the @dataattribute of my SpatialLinesDataFrame

Clipped_Street_2@data<-sp::merge(Clipped_Street_2@data,Points_agg,by.x="identifier",by.y="nearest_line_id", all=TRUE )

Now I want to split SpatialLinesDataFrame by the Crime type column so that I can use it in leaflet map control function using the following script.

map<-leaflet(Clipped_Street_2) %>%addTiles()

names(Clipped_Street_2) %>%
  purrr::walk( function(df) {
        map <<- map %>%
      addCircles(data=Clipped_Street_2[[df]],
                 lng=~Longitude , 
                 lat=~Latitude,
                 weight=~n,
                 label=~df,
                 color = ~pal(`Crime type`),
                 group = ~df)
  })

%>%
  addLayersControl(
    overlayGroups = names(Clipped_Street_2),
    options = layersControlOptions(collapsed = FALSE))

My problem is the splitting doesn't appear to work. It does for simple point data sets and I was wondering if there is a function, method or tool I am missing to split this dataset. I am currently working with a simple split function but I get an error (see below).

Clipped_Street_2 <- split(Clipped_Street_2@data, Clipped_Street_2@data$`Crime type`)

Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) : 
  group length is 0 but data length > 0

Not really sure what to do next, but I will post any progress I make myself. This solution seemed sensible, but I couldn't make it work in my situation.

0

You can do this via an index in a for loop or lapply. For simplicity's sake I will show a for loop.

street2 <- list() 
  for(i in unique(Clipped_Street$Crime type)) {
    street2[[i]] <- street[street$Crime type == i,] 
 }

This will result in the same type of output as split, a list with a SpatialLinesDataFrame, representing the subset attribute, for each list element.

  • Thanks @jeffrey-evans I will toy around with the loop! – Jacob Baisley Sep 20 at 8:11
  • I can split the dataframe as a flat dataframe. However, that creates a new problem of associating the spatial lines back in to a split dataframe. Otherwise, trying to split a spatial lines data frame I get the following error Error in FUN(X[[i]], ...) : object not a >= 2-column array – Jacob Baisley Sep 23 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.