0

I'm currently desperate for a filtering in R. I have searched the forum for a long time, but have not found a corresponding question.

I have the following record:

"Latitude","Longitude","Land","Stadt"
37.7749295,-122.4194155,"US","San Francisco"
48.8529682,2.3499021,"FR","Paris"
28.6139391,77.2090212,"IN","Paharganj"
48.801408,2.130122,"FR","Versailles"
26.9124336,75.7872709,"IN","Jaipur"
43.296482,5.36978,"FR","Marseille"
26.0173274,76.5025742,"IN","Khilchipur"
37.8651011,-119.5383294,"US","White Wolf"
45.4396385,10.6865449,"IT","Peschiera del Garda"
12.5683747,99.9576888,"TH","Hua Hin"
27.1766701,78.0080745,"IN","Agra"

Now I want to consider only the data from India (IN). But the filtering has to be done via longitude and latitude and not via country. This means that I need all data within the following coordinates:

Latitude > 26 and < 29 AND Longitude > 75 and < 79

Accordingly, the following data rows should come as the result:

"Latitude","Longitude","Land","Stadt"
28.6139391,77.2090212,"IN","Paharganj"
26.9124336,75.7872709,"IN","Jaipur"
26.0173274,76.5025742,"IN","Khilchipur"
27.1766701,78.0080745,"IN","Agra"

What would be the right way to do this double filtering via Longitude and Latitude?

I would be very grateful for any help.

Best regards, Jeem

jeem67
  • 29
  • 1
  • 1
  • 7
  • 1
    You want something like `Latitude > 26 & Latitude < 29 & Longitude > 75 & Longitude < 79` but it is difficult to tell you anything more without a [reproducible example](https://stackoverflow.com/a/5963610/8918309). Is your data in a `data.frame`? `data.table`? list? – MRau Jan 25 '19 at 16:56
  • Hi MRau, thanks for your answer. The data are in a data table. I have a connection to MongoDB. But I can also write the data into a data.frame after loading the data, if that makes more sense for filtering. – jeem67 Jan 25 '19 at 16:59
  • 1
    No, `data.table` is great for that. Something like that should work: `dt_filtered <- dt[Latitude > 26 & Latitude < 29 & Longitude > 75 & Longitude < 79, ]` – MRau Jan 25 '19 at 17:05
  • 1
    Or, even better, with a `data.table` you can use the `%between%` operator: `dt_filtered <- dt[Latitude %between% c(26, 29) & Longitude %between% c(75, 79), ]` – MRau Jan 25 '19 at 17:08
  • Great. The first alternative worked. I get the following error with the second alternative --> could not find function "%between%" although I installed the package dplyr. – jeem67 Jan 25 '19 at 17:26
  • 1
    Check `?between` and try `between(x, lower, upper)`. Otherwise I'm afraid I can't help you, maybe it because of your R/data.table version. – MRau Jan 26 '19 at 14:02

0 Answers0