I have a time series, which measures a variable X each second and changes the GPS coordinates of the readings every 10 seconds. When I aggregate based on the GPS coordinates I loose the order of the time series, as in the following example:
Input (loaded as variable stack in R):
time,latitude,longitude,sensor_reading
20150220081613,55.9516598,-3.2212117,2
20150220081614,55.9516598,-3.2212117,3
20150220081621,55.9516891,-3.2208011,5
20150220081622,55.9516891,-3.2208011,3
20150220081630,55.9516739,-3.2207998,2
20150220081631,55.9516739,-3.2207998,3
Then I run the following command:
aggregate(stack, by = list(stack$longitude, stack$latitude), FUN = mean)
And I get the following output, which has lost the order of the time series:
Group.1 Group.2 time latitude longitude sensor_reading
1 -3.221212 55.95166 2.015022e+13 55.95166 -3.221212 2.5
2 -3.220800 55.95167 2.015022e+13 55.95167 -3.220800 2.5
3 -3.220801 55.95169 2.015022e+13 55.95169 -3.220801 4.0
I solved the problem by indexing the rows and then sorting on their aggregates.
aggregate_file <- function(file, fun){
# Get rows in order to sort after aggregation
file$rows <- as.numeric(row.names(file))
file_agg <- aggregate(file, by = list(file$longi, file$latitude), FUN = fun)
# Order based on aggregates of the rows
file_agg_ordered <- file_agg[order(file_agg$rows),]
file_agg_ordered
}
Is there a more elegant solution with less code?