Adam Ralphus
Adam Ralphus

Reputation: 339

if-else conditions produce odd results

I am running this piece of code in R:

set.seed(4997)
n =50
data1 <- runif(n, min = 0, max = 1)
data2 <- runif(n, min = 2, max = 5)
sample.data <- c(data1,data2)
sample.data
y <- numeric(2*n)
  for(i in 1:n){
    if((sample.data[i] < 1)) {y[i] <- -1
    } else if ((sample.data[i] > 1))  {y[i] <- 1}}
y

Basically, what I am trying to do is to assign a value for "y" based on the value at each index of "sample.data".

My logic seems legit, but the output is not what I expected:

> y
  [1] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 [29] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1  0  0  0  0  0  0
 [57]  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 [85]  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

I do not even know how the zero comes in place.

Any tip would be great! Thanks!

Upvotes: 1

Views: 45

Answers (2)

NM_
NM_

Reputation: 1999

Your vector sample data is of size 2*n so your for loop should be

for(i in 1: (2*n)){
    if((sample.data[i] < 1))
     {y[i] <- -1
} else if ((sample.data[i] > 1)){
    y[i] <- 1}
}

> y
[1] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
[25] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
[49] -1 -1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
[73]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
[97]  1  1  1  1

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388797

That is because your n is 50 whereas length(sample.data) is 100. So when you loop from 1:n it iterates only for first 50 values and keeps remaining 50 values as it is (which is 0) .

What you needed was

for(i in 1:length(sample.data)) {
     if(sample.data[i] < 1) 
          y[i] <- -1
     else if (sample.data[i] > 1)  
          y[i] <- 1
}

However, you can avoid the for loop by using the vectorized ifelse and reduce the above code to just

ifelse(sample.data < 1, -1, 1)

Upvotes: 4

Related Questions