Consider the following data frame:
(tmp_df <-
structure(list(class = c(0L, 0L, 1L, 1L, 2L, 2L), logi = c(TRUE,
FALSE, TRUE, FALSE, TRUE, FALSE), val = c(1, 1, 1, 1, 1, 1),
taken = c(1.00684931506849, 0.993197278911565, 1.025, 0.975609756097561,
1.00826446280992, 0.991803278688525)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -6L), .Names = c("class",
"logi", "val", "taken")))
which creates:
Source: local data frame [6 x 4]
class logi val taken
<int> <lgl> <dbl> <dbl>
1 0 TRUE 1 1.0068493
2 0 FALSE 1 0.9931973
3 1 TRUE 1 1.0250000
4 1 FALSE 1 0.9756098
5 2 TRUE 1 1.0082645
6 2 FALSE 1 0.9918033
I wish to group by class, and if each group contains two members, then subtract 1 from val
if logi == FALSE
, otherwise, subtract the minimum value of taken
in that group from val
. If each group does not contain two members, then we subtract zero from val
.
Code using dplyr
package to do the above can be expressed using:
tmp_df %>%
group_by(class) %>%
mutate(taken_2 = ifelse(n() != 2, 0,
ifelse(logi, min(taken), 1)),
not_taken = val - taken_2)
However, this produces the incorrect result, where by the second ifelse
always resolves to the first condition:
Source: local data frame [6 x 6]
Groups: class [3]
class logi val taken taken_2 not_taken
<int> <lgl> <dbl> <dbl> <dbl> <dbl>
1 0 TRUE 1 1.0068493 0.9931973 0.006802721
2 0 FALSE 1 0.9931973 0.9931973 0.006802721
3 1 TRUE 1 1.0250000 0.9756098 0.024390244
4 1 FALSE 1 0.9756098 0.9756098 0.024390244
5 2 TRUE 1 1.0082645 0.9918033 0.008196721
6 2 FALSE 1 0.9918033 0.9918033 0.008196721
The correct result can be produced if we do not have the first ifelse
statement.
tmp_df %>%
group_by(class) %>%
mutate(taken_2 = ifelse(logi, min(taken), 1),
not_taken = val - taken_2)
producing:
Source: local data frame [6 x 6]
Groups: class [3]
class logi val taken taken_2 not_taken
<int> <lgl> <dbl> <dbl> <dbl> <dbl>
1 0 TRUE 1 1.0068493 0.9931973 0.006802721
2 0 FALSE 1 0.9931973 1.0000000 0.000000000 # correct!
3 1 TRUE 1 1.0250000 0.9756098 0.024390244
4 1 FALSE 1 0.9756098 1.0000000 0.000000000 # correct!
5 2 TRUE 1 1.0082645 0.9918033 0.008196721
6 2 FALSE 1 0.9918033 1.0000000 0.000000000 # correct!
We can see that this problem seems to be isolated to mutate
and the nested ifelse
by examining other code fragments that successfully do similar stuff:
tmp_df %>%
group_by(class) %>%
mutate(taken_2 = ifelse(n() != 3, 0,
ifelse(logi, min(taken), 1)),
not_taken = val - taken_2)
tmp_df_2 <-
tmp_df %>%
filter(row_number() <= 2)
(tmp_df_2$taken_2 <-
ifelse(c(0, 0), 0,
ifelse(tmp_df_2$logi, min(tmp_df_2$taken), 1)))
## but the following does not work (checks problem is not to do with grouping)
# tmp_df_2 %>%
# mutate(taken_2 = ifelse(n() != 2, 0,
# ifelse(logi, min(taken), 1)),
# not_taken = val - taken_2)
Why is this happening, and how can I obtain the expected behaviour? A workaround is to split the nested ifelse
logic into multiple in-line mutates:
tmp_df %>%
group_by(class) %>%
mutate(taken_2 = ifelse(n() != 2, 0, 1),
taken_3 = taken_2 * ifelse(logi, min(taken), 1),
not_taken = val - taken_3)
Someone else has identified a similar problem with nested ifelse but I don't know whether it has the same root: ifelse using dplyr results in NAs for some records