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.
Asked
Active
Viewed 251 times
1

josliber
- 43,891
- 12
- 98
- 133

eliran azulay
- 119
- 1
- 1
- 6
-
3Can you provide some example data? – Prasanna Nandakumar Apr 24 '15 at 13:38
-
2Please include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – figurine Apr 24 '15 at 13:41
-
2Please use `dput` to show the dataset. i.e Type `dput(head(yourdataset))` on your R console and copy/paste the output in your post – akrun Apr 24 '15 at 13:44
-
Try `aggregate(Speed~cbind(Distance=cut(DistancefromStart, breaks=seq(0,max(DistancefromStart), by=5000), include.lowest=TRUE), df1, FUN=mean)` (not tested) – akrun Apr 24 '15 at 13:49
1 Answers
1
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))]

akrun
- 874,273
- 37
- 540
- 662