eliran azulay
eliran azulay

Reputation: 119

aggregate by distance in R

I have a data set of road traffic data for certain roads, and I have a few columns in my data set. One of them is "Distance from start" (starts from 0 until the end of the road, in unit meters.), and another column is: "Speed". I want to find out the average speed along the road in intervals of about 5000 meters. How can I do it using aggregation or some other approch? enter image description here This is my data set, which has about 700,000 rows. It is sorted by DistanceFromStart.

Upvotes: 1

Views: 264

Answers (1)

akrun
akrun

Reputation: 887118

You may use cut to create the groups and then get the mean of "Speed"

library(data.table)
setDT(df1)[, list(Speed=mean(Speed)), by=list(cut(DistancefromStart,
   breaks= seq(0, max(DistancefromStart)+5000, by = 5000),
                 include.lowest=TRUE))] 

Upvotes: 1

Related Questions