# what is wrong with my code

#### Marwah Soliman

##### New Member
I'm generating a random numbers x,y between -10 and 10
then I create an if statement to classify each pair(x,y) in 4 quadrant in the following if statement
but the column quad give me all 4

so why?

Code:
x <- runif(n=400, min = -10, max = +10)
y <- runif(n=400, min = -10, max = +10)
mydata=cbind(x,y)
mydata=as.data.frame(mydata)
plot(x,y)
Code:
for(i in 1:400){

if((0< mydata$x[i] & mydata$x[i] <10) & (0< mydata$y[i] & mydata$y[i] <10)){
mydata$quad=1 }else if((0<mydata$x[i] & mydata$x[i]<10) & (-10<mydata$y[i] & mydata$y[i]<0)){ mydata$quad=2
}else
if((-10<mydata$x[i] & mydata$x[i]<0) & (-10<mydata$y[i] & mydata$y[i]<0)){
mydata$quad=3 }else if((-10<mydata$x[i] &mydata$x[i]<0) & (0<mydata$y[i] & mydata$y[i]<10)){ mydata$quad=4
}}

#### GretaGarbo

##### Human
I think you forgot to insert the "" in "mydata$quad=1" Code: set.seed(2103) #if you repeat the code you will get the same numbers x <- runif(n=400, min = -10, max = +10) y <- runif(n=400, min = -10, max = +10) mydata=cbind(x,y) mydata=as.data.frame(mydata) plot(x,y) # initialize the variable mydata$quad <- numeric(length = 400)

for(i in 1:400){

if((0< mydata$x[i] & mydata$x[i] <10) & (0< mydata$y[i] & mydata$y[i] <10)){
mydata$quad[i]=1 }else if((0<mydata$x[i] & mydata$x[i]<10) & (-10<mydata$y[i] & mydata$y[i]<0)){ mydata$quad[i]=2
}else
if((-10<mydata$x[i] & mydata$x[i]<0) & (-10<mydata$y[i] & mydata$y[i]<0)){
mydata$quad[i]=3 }else if((-10<mydata$x[i] &mydata$x[i]<0) & (0<mydata$y[i] & mydata$y[i]<10)){ mydata$quad[i]=4
}}

table(mydata$quad) Maybe this help text that I copied somewhere some time ago can be helpful: Code: # Your confusion comes from how SAS and R handle if-else constructions. # In R, if and else are not vectorized, meaning they check whether a single # condition is true (i.e., if("french"=="french") works) and cannot # handle multiple logicals (i.e., if(c("french","foreigner")=="french") doesn't work) # and R gives you the warning you're receiving. # By contrast, ifelse is vectorized, so it can take your vectors (aka input variables) # and test the logical condition on each of their elements, like you're used to in SAS. # An alternative way to wrap your head around this would be to build a loop using # if and else statements (as you've started to do here) but the vectorized ifelse # approach will be more efficient and involve generally less code. x<-c(3,6,9,12,15,21,25,32,36,39, 45, 70) group <- ifelse ((x >= 0 & x <= 10), 1 , ifelse((x >= 11 & x <= 20 ), 2 , ifelse((x >= 21 & x <= 30), 3 , ifelse((x >= 31 & x <= 40), 4 , ifelse((x >= 41 & x <= 50 ), 5 , 9 ) )))) An alternative (vectorized) method is: Code: mydata$quad <-
ifelse(((0< mydata$x & mydata$x <10) & (0< mydata$y & mydata$y <10)), 1,
ifelse(((0<mydata$x & mydata$x<10) & (-10<mydata$y & mydata$y<0)),  2,
ifelse(((-10<mydata$x & mydata$x<0) & (-10<mydata$y & mydata$y<0)), 3,
ifelse(((-10<mydata$x &mydata$x<0) & (0<mydata$y & mydata$y<10)),  4, 9))))

table(mydata\$quad)
Here there was no loop.