1

I have two dataset wanted to count match Ratio

> events_data       
  LONGITUDE LATITUDE  matchvalue
1     122.5    9.5    0.006269592
2     122.5   10.5    0.050156740
3     125.5   10.5    0.043887147
4     146.5   40.5    0.048213166
5     142.5   40.5    0.035078370
6     146.5   40.5    0.028213166

> events            
  LATITUDE LONGITUDE
1    9.880  124.1167
2   37.156  144.6611
  1. using events(within 5 both in latitude and longitude) to select data in events_data
  2. when events_data.matchvalue > 0.04, set this row data in events_data is ture, or is flase
  3. count match Ratio = (totalRows in events_data is ture)/(totalRows in events_data is ture + totalRows in events_data is false)

take event 1 for example

1. select data in events_data within latitude(9.880 +/- 5) and longitude(124.1167 +/-5),you can get data in events_data:
   LONGITUDE LATITUDE  matchvalue 
 1     122.5    9.5    0.006269592
 2     122.5   10.5    0.050156740
 3     125.5   10.5    0.043887147
2. count(events_data.matchvalue > 0.04) = 2
3. count match Ratio = 2/3 = 0.67

my expect is add a new column match_Ratio

  LATITUDE LONGITUDE  match_Ratio
1    9.880  124.1167  0.67
2   37.156  144.6611  0.33

my data used are:

events_data <- structure(list(LONGITUDE = c(122.5, 122.5, 122.5, 146.5, 142.5, 
146.5), LATITUDE = c(9.5, 10.5, 10.5, 40.5, 40.5, 40.5
), matchvalue = c(0.00626959247648903, 0.0501567398119122, 0.0438871473354232, 
0.0482131661442006, 0.0350783699059561, 0.0282131661442006)), .Names = c("LONGITUDE", 
"LATITUDE", "matchvalue"), row.names = c(NA, 6L), class = "data.frame")

events <- structure(list(LATITUDE = c(9.88, 37.156), LONGITUDE = c(124.1167, 
144.6611)), .Names = c("LATITUDE", "LONGITUDE"), class = "data.frame", row.names = 1:2)

How to implement it, thanks

3
  • If think there is an error in your post. count(events_data.matchvalue > 0.04) = 2 should be 3 as all of them are greater than 0.04. This would make the match_Ratio = 1. Dec 8, 2017 at 21:41
  • the first one is 0.006269592 < 0.04
    – Pan
    Dec 8, 2017 at 21:42
  • Ahhhhh, very nice... overlooked the extra zero. Dec 8, 2017 at 21:42

1 Answer 1

0

Here is a straightforward base R solution.

long <- events_data$LONGITUDE
lat <- events_data$LATITUDE
myVals <- events_data$matchvalue

events$match_Ratio <- apply(events, 1, function(x) {
    z <- which(lat > (x[1] - 5) & lat < (x[1] + 5))
    v <- which(long > (x[2] - 5) & long < (x[2] + 5))
    ind <- intersect(z, v)
    sum(myVals[ind] > 0.04)/length(ind)
})

events
  LATITUDE LONGITUDE match_Ratio
1    9.880  124.1167   0.6666667
2   37.156  144.6611   0.3333333
6
  • Thanks Joseph, if I add one condition TIME, t <- which(time > (x[3] - 15) & time < (x[3] + 15)), can I rewrite the statement like ind <- Reduce(intersect, list(z,v,t)) ?
    – Pan
    Dec 8, 2017 at 22:27
  • @Pan, yeah that will definitely work!! That is assuming you have a 3rd column for time in both data.frames. Dec 8, 2017 at 22:50
  • When I applied to my real data, it has errors: Error in x[3] - 5 : non-numeric argument to binary operator, x[3] represents LONGITUDE here, do you know why and how to fix it?
    – Pan
    Dec 9, 2017 at 22:41
  • @Pan, it is hard to see what the problem is without seeing some actual data that is representative of your real data. Generally speaking on SO, if you want good answers, you need to give all of the relevant information along with your attempted solution (this is lacking in your question above... other than this, you have a really well constructed question). If you'd like, you can edit your question above with more details regarding your situation along with what you have tried. Dec 9, 2017 at 23:13
  • @Pan, I wasn't trying to be harsh... I really want to help. I hope my previous comment did not discourage you from seeking additional help. Anywho, if you edit your question, I will try and get you a working solution. Dec 9, 2017 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.