Search code examples
rcoordinateslatitude-longitudespatialaggregation

aggregate the points that have same coordinates in one point


I have a question with regard to spatial aggregation in R. My dataset has latitude/longitude coordinates some close to each other and some are not. I want to make one point for the latitude/longitude coordinates that are close to each other.
I am unsure how to do this. Do I list the latitude/longitude coordinates as groups and make find the mean to make one point represent each group? As I have little experience with this sort of stuff. I was hoping anyone of you might have some useful guidance/a possible solution.

Time Received   Speed   Latitude    Longitude
1.47917E+12      1.5    38.9295887  -77.2478945
1.47917E+12       1     38.9295048  -77.247922
1.47917E+12       3     38.9294865  -77.2479055
1.47917E+12       5     38.9294865  -77.2479055
1.47917E+12       2     38.9294771  -77.2478712
1.47917E+12       2     38.9294772  -77.2478712
1.47917E+12      1.5    38.9294771  -77.2477417
1.47917E+12      1.5    38.9294771  -77.2477417

For example if I can make the latitude/longitude coordinates below as one point:

     38.9294771 -77.2478712
     38.9294772 -77.2478712
     38.9294771 -77.2477417
     38.9294771 -77.2477417

Will be for example like below without effecting the Time,and Speed values:

38.9294771  -77.24774117

Solution

  • You can use the function given below that I found at round_any equivalent for dplyr? to round your coordinates to any desired precision. In the example below I use 0.1.

    fakedata <- data.frame(time = 1:100, 
                           speed = sample(3, 100, replace=TRUE),
                           latitude = seq(from=20, by=0.01, length.out=100),
                           longitude = seq(from=30, by=0.01, length.out=100))
    
    
    round_any = function(x, accuracy, f=round){f(x/ accuracy) * accuracy}
    
    library(dplyr)
    
    fakedata %>%
      mutate(latitude_round = round_any(latitude, accuracy = 0.1),
             longitude_round = round_any(longitude, accuracy = 0.1))
    

    Then use dplyr::group_by() to group by your rounded coordinates and aggregate the time and speed values however you want.