mercredi 20 mai 2020

Nested if else statement in R - 'else' statement executed even when 'if' statement is TRUE

I have a dataframe with 2 columns: df$a and df$b. I need to calculate the values for column df$c based on the values of df$b using 2 seperate sets of conditions. Which set of conditions should be applied depends on the value of df$a.

I tried to solve this by writing a nested if else statement.

# A subset of my data
    a <- c(4211L, 2660L, 2839L, 3967L, 3167L, 2755L, 1680L, 2400L, 1173L, 1301L, 2370L, 2366L, 411L, 615L, 1382L, 826L, 717L, 401L, 177L, 82L, 579L, 246L)
    b <- c(0.213, 0.102, 0.092, 0.121, 0.093, 0.0918, 0.0241, 0.060, 0.008, 0.003, 0.0385, 0.0368, -0.0529, -0.0697, 0.0192, -0.0346, -0.053, NA, -0.098, -0.139, -0.137, -0.0697)
    df <- data.frame(a,b)

I want to use the first set of conditions when df$a <1000, and the second set of conditions when df$a>=1000. This is my code:

df$c <- if (df$a < 1000) {
          ifelse(df$b <= -0.2, '1',
                 ifelse(df$b > -0.2 & df$b <= -0.1, '2',
                        ifelse(df$b > -0.1 & df$b <= 0.0, '3',
                               ifelse(df$b > 0.0 & df$b <= 0.1, '4',
                                      '5'))))
        } else {
          ifelse(df$b <= 0.0, '1',
                 ifelse(df$b > 0.0 & df$b <= 0.1, '2',
                        ifelse(df$b > 0.1 & df$b <= 0.2, '3',
                               ifelse(df$b > 0.2 & df$b <= 0.3, '4',
                                      '5'))))
        }

However, the code calculates all df$c values based on conditions in the else statement, even when (df$a < 1000) is TRUE. Does anyone know what is causing this mistake? I get the following warning message:

Warning message:
In if (df$a < 1000) { :
  the condition has length > 1 and only the first element will be used

Aucun commentaire:

Enregistrer un commentaire