Preslava Michorova
Preslava Michorova

Reputation: 11

What could be the reason for an if- statement in an R loop to only consider the first condition of the loop?

I spent a few hours trying to figure out why this loop isn't working. It keeps on printing '1'. The dataset used contains combinations of the 4 variables, though it seems the unsuccessful loop covers the first one only:

data$newcolumn <- for (newcolumn in 1:nrow(data)) 
    {if(data$Male_exposure=="High" && data$Quality=="HighQ") {print(1)}
       if (data$Male_exposure=="High"&& data$Quality=="LowQ") {print(2)}
       if (data$Male_exposure=="Low"&& data$Quality=="HighQ") {print(3)}
       if (data$Male_exposure=="Low"&& data$Quality=="LowQ") {print(4)}}

Why could that be the case?

Upvotes: 0

Views: 25

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521794

You should be using ifelse here, which is already vectorized. But, there is a potentially cleaner way of expressing your logic using case_when from the dplyr package:

library(dplyr)

data$newcolumn <- case_when(
    data$Male_exposure == "High" && data$Quality == "HighQ" ~ 1,
    data$Male_exposure == "High" && data$Quality == "LowQ"  ~ 2,
    data$Male_exposure == "Low"  && data$Quality == "HighQ" ~ 3,
    data$Male_exposure == "Low"  && data$Quality == "LowQ"  ~ 4
    TRUE ~ 5
)

If you really wanted to continue with your loop approach, then you would need to actually make use of the loop counter inside the loop's code:

data$newcolumn <- for (i in 1:nrow(data)) {
    if (data[i, "Male_exposure"] == "High" && data[i, "Quality"] == "HighQ") {
        print(1)
    }
    if (data[i, "Male_exposure"] == "High" && data[i, "Quality"] == "LowQ") {
        print(2)
    }
    if (data[i, "Male_exposure"] == "Low" && data[i, "Quality"] == "HighQ") {
        print(3)
    }
    if (data[i, "Male_exposure"] == "Low" && data[i, "Quality"] == "LowQ") {
        print(4)
    }
}

Upvotes: 1

Related Questions