r - dplyr top_n remove 0 in summary -
how remove 0 in table summary when using top_n dplyr?
library(ggplot2) library(dplyr) data("diamonds") diamonds #set diamonds data.frame manualtest = diamonds %>% count(cut) %>% top_n(3) table(manualtest$cut)
result
fair good premium ideal 0 0 1 1 1
expected result
premium ideal 1 1 1
if str(manualtest)
see manualtest$cut
factor
variable. solution make character. 3 options
manualtest = diamonds %>% count(cut) %>% mutate(cut = as.character(cut)) %>% top_n(3)
2) manualtest$cut <- as.character(manualtest$cut)
then run table(manualtest$cut)
table(as.character(manualtest$cut))
Comments
Post a Comment