如何在使用top_n和dplyr时删除表摘要中的0?
library(ggplot2)
library(dplyr)
data("diamonds")
diamonds #set diamonds as data.frame
manualTest = diamonds %>%
count(cut) %>%
top_n(3)
table(manualTest$cut)
结果
Fair Good Very Good Premium Ideal
0 0 1 1 1
预期结果
Very Good Premium Ideal
1 1 1
答案 0 :(得分:0)
如果您执行str(manualTest)
,则会看到manualTest$cut
是factor
变量。所以解决方案就是让它成为一个角色。三个选项
manualTest = diamonds %>%
count(cut) %>% mutate(cut = as.character(cut)) %>%
top_n(3)
2)
manualTest$cut <- as.character(manualTest$cut)
然后运行table(manualTest$cut)
table(as.character(manualTest$cut))