Learn R Programming

stplanr (version 0.6.2)

onewayid: Aggregate ods so they become non-directional

Description

For example, sum total travel in both directions.

Usage

onewayid(
  x,
  attrib,
  id1 = names(x)[1],
  id2 = names(x)[2],
  stplanr.key = od_id_order(x, id1, id2)
)

# S3 method for data.frame onewayid( x, attrib, id1 = names(x)[1], id2 = names(x)[2], stplanr.key = od_id_order(x, id1, id2) )

# S3 method for SpatialLines onewayid( x, attrib, id1 = names(x)[1], id2 = names(x)[2], stplanr.key = od_id_order(x, id1, id2) )

Arguments

x

A data frame or SpatialLinesDataFrame, representing an OD matrix

attrib

A vector of column numbers or names for deciding which attribute(s) of class numeric to aggregate

id1

Optional (it is assumed to be the first column) text string referring to the name of the variable containing the unique id of the origin

id2

Optional (it is assumed to be the second column) text string referring to the name of the variable containing the unique id of the destination

stplanr.key

A key of unique OD pairs regardless of the order, autogenerated by od_id_order()

Value

onewayid outputs a data.frame with rows containing results for the user-selected attribute values that have been aggregated.

Details

Flow data often contains movement in two directions: from point A to point B and then from B to A. This can be problematic for transport planning, because the magnitude of flow along a route can be masked by flows the other direction. If only the largest flow in either direction is captured in an analysis, for example, the true extent of travel will by heavily under-estimated for OD pairs which have similar amounts of travel in both directions. Flows in both direction are often represented by overlapping lines with identical geometries (see flowlines()) which can be confusing for users and are difficult to plot.

See Also

Other lines: angle_diff(), geo_toptail(), is_linepoint(), line2df(), line2points(), line_bearing(), line_match(), line_midpoint(), line_sample(), line_segment(), line_via(), mats2line(), n_sample_length(), n_vertices(), onewaygeo(), points2line(), toptail_buff(), toptailgs(), update_line_geometry()

Examples

Run this code
# NOT RUN {
# # this function is deprecated so examples are not expected to run
# # keeping the example code in there for now for posterity
# flow_oneway <- onewayid(flow, attrib = 3)
# nrow(flow_oneway) < nrow(flow) # result has fewer rows
# sum(flow$All) == sum(flow_oneway$All) # but the same total flow
# # using names instead of index for attribute
# onewayid(flow, attrib = "All")
# # using many attributes to aggregate
# attrib <- which(vapply(flow, is.numeric, TRUE))
# flow_oneway <- onewayid(flow, attrib = attrib)
# colSums(flow_oneway[attrib]) == colSums(flow[attrib]) # test if the colSums are equal
# # Demonstrate the results from onewayid and onewaygeo are identical
# flow_oneway_geo <- onewaygeo(flowlines, attrib = attrib)
# plot(flow_oneway$All, flow_oneway_geo$All)
# flow_oneway_sf <- onewayid(flowlines_sf, 3)
# plot(flow_oneway_geo, lwd = flow_oneway_geo$All / mean(flow_oneway_geo$All))
# plot(flow_oneway_sf$geometry, lwd = flow_oneway_sf$All / mean(flow_oneway_sf$All))
# with spatial data
data(flowlines)
fo <- onewayid(flowlines, attrib = "All")
head(fo@data)
plot(fo)
sum(fo$All) == sum(flowlines$All)
# test results for one line
n <- 3
plot(fo[n, ], lwd = 20, add = TRUE)
f_over_n <- rgeos::gEquals(fo[n, ], flowlines["All"], byid = TRUE)
sum(flowlines$All[f_over_n]) == sum(fo$All[n]) # check aggregation worked
plot(flowlines[which(f_over_n)[1], ], add = TRUE, col = "white", lwd = 10)
plot(flowlines[which(f_over_n)[2], ], add = TRUE, lwd = 5)
# }

Run the code above in your browser using DataLab