na.rm = TRUE seems to not work for a tibble with the datatype of pillar_num

when I use num function to set the tibble digits fixed to 2, and then sum the column with NA values.
But the result is NA, even if the na.rm = TRUE.

library(tidyverse)

test_tb <- tibble(
    data = c(10.0, 10.1562, NA)
)
test_tb %>%
    mutate(data = num(data, digits = 2)) %>%
    summarise(sum_data = sum(data, na.rm = TRUE))

the result is NA
image

Hi @bvb_lc ,

Have you checked the input requirements of the sum() function?

it looks to me like a bug, or at least something pillar would want to know about (if they dont already) so I raised an issue on their forum here : pillar::num type vector is not properly treated by base::sum (with respect to na.rm=TRUE) · Issue #659 · r-lib/pillar (github.com)

in case you want to keep using pillar::num in the short term; you can skirt around this by using na.omit() instead of na.rm=TRUE
i.e

test_tb %>%
    mutate(data = num(data, digits = 2)) %>%
    summarise(sum_data = sum(na.omit(data)))
3 Likes

Thank you, the na.omit function really works.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.