dplyr error: strange issue when combining group_by, mutate and ifelse. Is it a bug?

前端 未结 1 1307
一向
一向 2020-12-29 05:38

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         


        
相关标签:
1条回答
  • 2020-12-29 06:08

    Wrap it all in as.numeric to force the output format so the NAs, 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"
    
    0 讨论(0)
提交回复
热议问题