I want to associate points in these coordinates and associate them with the correspondent number. However every time I tried to run it I got this: the condition has length > 1
for (i in dados) {
if (dados[dados$latitude>="55.84" & dados$latitude<= "55.95" & dados$longitude>="-3.444" & dados$longitude<="-3.198", ]){
dados$neighbourhood <- 1
} else if (dados[dados$latitude>="55.84" & dados$latitude<= "55.95" & dados$longitude>="-3.198" & dados$longitude<="-3.058", ]){
dados$neighbourhood <- 2
} else if (dados[dados$latitude>="55.95" & dados$latitude<="56.01" & dados$longitude>="-3.444" & dados$longitude<="-3.198", ]){
dados$neighbourhood <- 3
} else if (dados[dados$latitude>="55.95" & dados$latitude<="56.01" & dados$longitude>="-3.189" & dados$longitude<="-3.058", ]){
dados$neigbourhod <- 4
}
}
Your help would be much appreciated thanks in advance.
CodePudding user response:
Often, you need to execute some statements only when some condition is met. You can use following conditional statements in your code to do this.
if Statement: use it to execute a block of code, if a specified condition is true else Statement: use it to execute a block of code, if the same condition is false else if Statement: use it to specify a new condition to test, if the first condition is false ifelse() Function: use it when to check the condition for every element of a vector
CodePudding user response:
Here is a vectorized way. Use the fact that binary numbers are written with 0/1
times powers of two. And add 1 to have the results be one-based.
i2 <- dados$latitude >= 55.95 & dados$latitude<= 56.01
i1 <- dados$longitude >= -3.198 & dados$longitude <= -3.058
dados$neigbourhod <- 1 i1 i2*2