Reputation: 119
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?
This is my data set, which has about 700,000 rows. It is sorted by DistanceFromStart.
Upvotes: 1
Views: 264
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