I am having strange issues with dplyr and combination of group_by, mutate and ifelse. Consider the following data.frame
> df1
crawl.id group.id hits.dif
Wrap it all in as.numeric
to force the output format so the NA
s, which are logical
by default, don't override the class of the output variable:
df1 %>%
group_by(group.id) %>%
mutate( hits.consumed = as.numeric(ifelse(hits.diff<=0,-hits.diff,0)) )
# crawl.id group.id hits.diff hits.consumed
#1 1 1 NA NA
#2 1 2 NA NA
#3 2 2 0 0
#4 1 3 NA NA
#5 1 3 NA NA
#6 1 3 NA NA
Pretty sure this is the same issue as here: Custom sum function in dplyr returns inconsistent results , as this result suggests:
out <- df1[1:2,] %>% mutate( hits.consumed = ifelse(hits.diff <= 0, -hits.diff, 0))
class(out$hits.consumed)
#[1] "logical"
out <- df1[1:3,] %>% mutate( hits.consumed = ifelse(hits.diff <= 0, -hits.diff, 0))
class(out$hits.consumed)
#[1] "numeric"