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

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

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -