Jeff Swanson
Jeff Swanson

Reputation: 45

dplyr if_else Error: `true` must be length x (length of `condition`) or one, not 0 in r

I have a dataframe that looks something like this:

df <- data.frame('Home.Team'=c("Omaha South", "Millard North", "Elkhorn","Elkhorn"),                      
                 'Winner'=c("Omaha South", "Millard North", "Elkhorn","Elkhorn"),
                 'Won By'=c(8,22,4,30),
                 'Away Class'=c("TRUE", "FALSE", "TRUE", "FALSE"))

I'm trying to create a new column/variable using conditional if_else from dplyr. This had worked for me in the past but for some reason it is now giving me an error. Below is the r code and error:

df$'Pre Score' <- 
        if_else(df$`Away Class`=="FALSE", 
        if_else(df$Home.Team==df$Winner, .8 + (df$`Won By`/100) -1, -1.2 - (df$`Won By`/100) -1), 
        if_else(df$Home.Team==df$Winner, .8 + (df$`Won By`/100), -1.2 - (df$`Won By`/100)))

Error: true must be length 4 (length of condition) or one, not 0

I've read through multiple SO discussions (example, example, example) related to this but haven't been able to translate it into a solution for my problem. It seem to have something to do with the "if true" portion of the code. Apparently it thinks this is a length of one whereas I want it to be a length of 4, or to work for all rows. Tried replacing if_else with case_when but wasn't able to succeed there either.

Upvotes: 0

Views: 6919

Answers (1)

iod
iod

Reputation: 7592

This should have been a comment, but it came out too confusing:

You're missing periods in your variable names - data.frame automatically adds those when you create a df with variable names that have spaces:

if_else(df$`Away.Class`=="FALSE", #Away.Class instead of `Away Class`
             if_else(df$Home.Team==df$Winner, .8 + (df$`Won.By`/100) -1, -1.2 - (df$`Won.By`/100) -1), # Won.By instead of `Won By`
             if_else(df$Home.Team==df$Winner, .8 + (df$`Won.By`/100), -1.2 - (df$`Won.By`/100))) # ditto
[1] 0.88 0.02 0.84 0.10

Here's why your code results in an error: when you run .8 + (df$Won By/100) -1, the result is NULL, because the column doesn't exist - so the list TRUE/FALSE results is length zero. ifelse needs this list to either be the same length as your condition (which is four, in which case each TRUE case will get it appropriate data), or 1 (in which case all TRUE results will get the same output).

Upvotes: 2

Related Questions