0

I have latitude and longitude data such as...

lat=runif(1000, min = 22.1, max = 22.8)
lon=runif(1000, min = 120, max = 120.6)
latlon=cbind(lat,lon)

and I want to classify them into different zones based on this...

Zone=c("A","B","C","C","C","D","E","E","E")
North=c(23,23,22.83,22.67,22.42,22.33,22.33,22.42,22.50)
South=c(22.67,22.67,22.67,22.33,22.33,22.08,22.08,22.33,22.42)
East=c(120,120.2,120.2,120.3,120.4,120.5,120.7,120.6,120.5)
West=c(119.7,120,120.2,120,120.3,120.2,120.5,120.4,120.3)
data.frame(cbind(Zone,North,South,East,West))

https://i.stack.imgur.com/dJahr.jpg

I want to separate my gis data latloninto 5 different zone (A,B,C,D,E) and I tried to use ifelse before, but it seems to be not very efficiently

Anyway I can finish it more quickly?

===================== I write a new code based on Adrian Martin's suggestion

newzone=case_when(lat >= zzz$South[1] & lat <= zzz$North[1] & lon <= zzz$East[1] & lon >= zzz$West[1]~ zzz$Zone[1],
           lat >= zzz$South[2] & lat <= zzz$North[2] & lon <= zzz$East[2] & lon >= zzz$West[2]~ zzz$Zone[2],
           lat >= zzz$South[3] & lat <= zzz$North[3] & lon <= zzz$East[3] & lon >= zzz$West[3]~ zzz$Zone[3],
           lat >= zzz$South[4] & lat <= zzz$North[4] & lon <= zzz$East[4] & lon >= zzz$West[4]~ zzz$Zone[4],
           lat >= zzz$South[5] & lat <= zzz$North[5] & lon <= zzz$East[5] & lon >= zzz$West[5]~ zzz$Zone[5],
           lat >= zzz$South[6] & lat <= zzz$North[6] & lon <= zzz$East[6] & lon >= zzz$West[6]~ zzz$Zone[6],
           lat >= zzz$South[7] & lat <= zzz$North[7] & lon <= zzz$East[7] & lon >= zzz$West[7]~ zzz$Zone[7],
           lat >= zzz$South[8] & lat <= zzz$North[8] & lon <= zzz$East[8] & lon >= zzz$West[8]~ zzz$Zone[8],
           lat >= zzz$South[9] & lat <= zzz$North[9] & lon <= zzz$East[9] & lon >= zzz$West[9]~ zzz$Zone[9])

Is there anyway I can make the code more simpe? Thanks

  • 1
    can you post a reproducible sample of the data ? – Mouad_Seridi May 15 '17 at 15:49
  • 2
    Please provide the code you have developed. It will make others easier to improve your code. – www May 15 '17 at 15:50
  • Please don't post images of code; it's not difficult to post the data directly. Some good examples of how to do it to make it easier on answerers can be found [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – r2evans May 15 '17 at 16:09
  • I don't want to "cut" my lon and lat data into grid. It is a complete different question. The answer came from Adrian Martin is more appropriate for me. Thanks your help, anyway. – Chien-Pang Chin May 16 '17 at 03:28

1 Answers1

0

cut() (the answer linked to in r2evans' comment) will work if you're slicing into 5 sections of equal width or whatever, or if you have some vector of proportions you're trying to match. If you're trying to match already existing lat/lon coordinates (as seems likely since you're trying to use ifelse), I'd use dplyr's function case_when().

    library(dplyr)
    latlon <- bind_cols(lat, lon) %>%
        mutate(Zone = case_when(.$lat > 22.83 & .$lon > 120.2 ~ A,
                                .$lat > 22.42 & .$lon > 120.0 ~ B,
                               etc etc)

case_when is how to get ifelse-type statements into a dplyr chain. It runs quicker that ifelse, I believe.

Adrian Martin
  • 780
  • 7
  • 21