0

I have a tibble with logical data form that I am trying to summarise.

>test

# A tibble: 17 x 1
test 
<lgl>
1 NA   
2 FALSE
3 FALSE
4 FALSE
5 FALSE
6 FALSE
7 FALSE
8 FALSE
9 TRUE 
10 FALSE
11 FALSE
12 FALSE
13 FALSE
14 FALSE
15 FALSE

Piping this into a summary function works for checking NAs

> test %>% summarise(sum(is.na(test)))
# A tibble: 1 x 1
  `sum(is.na(test))`
               <int>
1                  1

However I can't get it to work for testing for FALSE or TRUE

> test %>% summarise(sum(test==TRUE))
# A tibble: 1 x 1
  `sum(test == TRUE)`
                <int>
1                  NA

> test %>% summarise(sum(test==FALSE))
# A tibble: 1 x 1
  `sum(test == FALSE)`
                 <int>
1                   NA
Clem Manger
  • 173
  • 1
  • 12
  • Well, you are `sum`ming over logical values with `NA`'s you need to remove them. `test %>% summarise(sum(test, na.rm = TRUE))` should also work. – Ronak Shah Jul 20 '18 at 09:50

1 Answers1

0

This is because NA's are part of the summation.

Hadley Wickham suggests a fix here https://github.com/tidyverse/dplyr/issues/539

test %>% filter(!is.na(test)) %>% summarise(sum(test==FALSE))

# A tibble: 1 x 1
  `sum(test == FALSE)`
                 <int>
1                   15

I hope this saves someone else some time!

Clem Manger
  • 173
  • 1
  • 12