[R-sig-Geo] Aggregating points based on distance

Andy Bunn bunn@ @end|ng |rom wwu@edu
Wed Mar 13 19:13:55 CET 2019


I would like to create averages of all the variables in a SpatialPointsDataFrame when points are within a specified distance of each other. I have a method for doing this but it seems like a silly way to approach the problem. Any ideas for doing this using modern syntax (especially of the tidy variety) would be appreciated.
    

To start, I have a SpatialPointsDataFrame with several variables measured for each point. I'd like to get an average value for each variable for points within a specified distance. E.g., getting average cadmium values from the meuse data for points within 100 m of each other:
    
    library(sf)
    library(sp)
    data(meuse)
    pts <- st_as_sf(meuse, coords = c("x", "y"), remove=FALSE)
    pts100 <- st_is_within_distance(pts, dist = 100)
    # can use sapply to get mean of a variable. E.g., cadmium
    sapply(pts100, function(x){ mean(pts$cadmium[x]) })

Above, I've figured out how to use sapply to do this variable by variable. So I could, if I wanted, calculate the mean for each variable, generate a centroid for each point and then a SpatialPointsDataFrame of the unique values. E.g., for the first few variables:
    
    res <- data.frame(id=1:length(pts100),
                      x=NA, y=NA,
                      cadmium=NA, copper=NA, lead=NA)
    res$x <- sapply(pts100, function(p){ mean(pts$x[p]) })
    res$y <- sapply(pts100, function(p){ mean(pts$y[p]) })
    res$cadmium <- sapply(pts100, function(p){ mean(pts$cadmium[p]) })
    res$copper <- sapply(pts100, function(p){ mean(pts$copper[p]) })
    res$lead <- sapply(pts100, function(p){ mean(pts$lead[p]) })
    res2 <- res[duplicated(res$cadmium),]
    coordinates(res2) <- c("x","y")
    bubble(res2,"cadmium")
    
    
This works but seems cumbersome and like there must be a more efficient way. 
    
    
Thanks for any help, Andy
    
    



More information about the R-sig-Geo mailing list