Swiss12000 February 2016

dplyr top_n remove 0 in summary

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 

Answers


dimitris_ps February 2016

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))

Post Status

Asked in February 2016
Viewed 3,366 times
Voted 9
Answered 1 times

Search




Leave an answer