0

How to remove the 0 in the table summary when using top_n with dplyr?

library(ggplot2)
library(dplyr)
data("diamonds")
diamonds #set diamonds as data.frame

manualTest = diamonds %>%
  count(cut)  %>%
  top_n(3)


table(manualTest$cut)

Result

 Fair      Good Very Good   Premium     Ideal 
    0         0         1         1         1 

Expected result

 Very Good   Premium     Ideal 
  1         1         1 
dimitris_ps
  • 5,849
  • 3
  • 29
  • 55
S12000
  • 3,345
  • 12
  • 35
  • 51
  • 3
    Not sure is this related to `top_n`... Seems like `table(factor(manualTest$cut))` or something similar should do. Or you could do something like `count(cut = as.character(cut))` instead of `count(cut)`, depends on what your desired class. – David Arenburg Feb 08 '16 at 13:15

1 Answers1

0

If you do a str(manualTest) you will see that manualTest$cut is a factor variable. So the solution would be to make it a character. Three options

1)
manualTest = diamonds %>%
count(cut)  %>% mutate(cut = as.character(cut)) %>% 
top_n(3)
2)
manualTest$cut <- as.character(manualTest$cut)

then run table(manualTest$cut)

3)
table(as.character(manualTest$cut))
Community
  • 1
  • 1
dimitris_ps
  • 5,849
  • 3
  • 29
  • 55