Nicole Kappelhof
Nicole Kappelhof

Reputation: 65

ifelse not working in mutate if column not supplied in condition

It seems to me that if a column is not included in the conditional part of the ifelse statement, the ifelse statement in a dplyr mutate function does not work as expected:

mdf <- data.frame(a=c(1,2,3), b=c(3,4,5))

# this works:
> mdf %>% mutate(c=ifelse(a==1,0,1))
  a b c
1 1 3 0
2 2 4 1
3 3 5 1

# This does not work (expected column c to be equal to a):
> mdf %>% mutate(c=ifelse(0==1,0,a))
  a b c
1 1 3 1
2 2 4 1
3 3 5 1
# This does not work either (expected column c to be equal to a):
> mdf %>% mutate(c=ifelse("a" %in% names(.),a,0))
  a b c
1 1 3 1
2 2 4 1
3 3 5 1

If using a regular if-statement, it does work:

> mdf %>% mutate(c=if("a" %in% names(.)){a}else{1})
  a b c
1 1 3 1
2 2 4 2
3 3 5 3

However, I was hoping to use the ifelse statement, since it has a cleaner syntax. Is there a way to achieve the desired result with the ifelse statement?

Upvotes: 0

Views: 741

Answers (1)

Nicole Kappelhof
Nicole Kappelhof

Reputation: 65

I see that the length of the conditional statement determines what is returned. If the condition evaluates to one True/False value, it only returns one value (instead of the entire column). The value returned seems to be the first value of the desired column:

> mdf %>% mutate(c=ifelse("a" %in% names(.),a,0))
  a b c
1 1 3 1
2 2 4 1
3 3 5 1

If I increase the lenght of the condition to the number of rows, the ifelse statement will return the entire column:

> mdf %>% mutate(c=ifelse(rep("a", nrow(.)) %in% names(.),a,0))
  a b c
1 1 3 1
2 2 4 2
3 3 5 3

Upvotes: 1

Related Questions