0

I have multiple locations for each action performed by a user in my database as shown below. I want to get the mean or centre of these distances to use as the single location for each user.

Action  location.lon    location.lat

user1   -6.2346259      53.3371473
user1    0              0
user4   -6.22550044     53.59729241
user2   -6.262663209    53.33398243
user2   -6.289571616    53.32012803
user3    8.6388684      49.3024665
user5   -80.434882      39.2474397
user3   -2.460740516    52.60026199
user3   -122.5168562   -37.92878211

I have this information in R and mongodb, so I will be happy with suggestions in both.

Thank you in advance.

Cormac
  • 39
  • 1
  • 6
  • You can treat the locations for a single user as polygon which you can find [centre of gravity](http://stackoverflow.com/questions/5271583/center-of-gravity-of-a-polygon) of. – Hans Kristian Feb 21 '14 at 09:56

1 Answers1

0

This will give you simple means. If you are looking for centre of area, you need a more complex formula in summarise:

df<-read.table(header=T,text="Action  location.lon    location.lat
user1   -6.2346259      53.3371473
user1    0              0
user4   -6.22550044     53.59729241
user2   -6.262663209    53.33398243
user2   -6.289571616    53.32012803
user3    8.6388684      49.3024665
user5   -80.434882      39.2474397
user3   -2.460740516    52.60026199
user3   -122.5168562   -37.92878211")

require(dplyr)

group_by(df,Action) %.%
  summarise(mean.lon=mean(location.lon),mean.lat=mean(location.lat))

  Action   mean.lon mean.lat
1  user1  -3.117313 26.66857
2  user2  -6.276117 53.32706
3  user3 -38.779576 21.32465
4  user4  -6.225500 53.59729
5  user5 -80.434882 39.24744
Troy
  • 8,581
  • 29
  • 32
  • Thank you, but I am getting mostly NA values in my resulting dataframe. Do you know why this may be? – Cormac Feb 21 '14 at 11:43
  • Not sure - do you have NAs in your original dataset? – Troy Feb 21 '14 at 11:55
  • I have very few NAs in the original dataset but way more in the new dataframe. A lot of users may have only one record also, if that has any affect? – Cormac Feb 21 '14 at 11:59