Search code examples
rdataframecountuniquemean

Attaching mean Longitude and Latitude to specific counted labels


I have a dataset of crimes in London and I made a subset of it which resulted in the columns: long, lat, LSOA.code . (820 entries) I want mean long and lats of the 32(found out by count function)- different LSOA.codes. How do I specify this?

#subset data to relevant long-lat and type of polygon
london_crim_sub = subset(london_crim, select = c(5,6,8))
#remove missing
london_crim_sub = remove_missing(london_crim_sub)

library(dplyr)

DF = data.frame(count(london_crim_sub$LSOA.code))

So i want a dataframe like this: columns: LSOA.code, freq, meanlon, meanlat

I found the freq, using the count. Now i only need to get the unique mean long and lat per LSOA.code with different freq.


Solution

  • If I understand you right, you want to group by the LSOA.code and produce mean latitude and longitude? We can update the DF dataframe you've already created:

    DF$meanlat <- aggregate(lat ~ LSOA.code, london_crim_sub, mean)
    DF$meanlon <- aggregate(lon ~ LSOA.code, london_crim_sub, mean)