我想在filter()之后完全删除零行的类别。
data("mtcars")
df1<-mtcars %>%
mutate(
gearlevel=factor(gear))%>%
filter(gear!="3")
table(df1$gearlevel)
3 4 5
0 12 5
我要删除“ 3”类别。我制定了一个笨拙的解决方案:
data("mtcars")
df1<-mtcars %>%
mutate(
gearlevel=factor(gear)) %>%
filter(gearlevel!="3") %>%
mutate(
gearlevel = factor(gearlevel, levels = c("4", "5"))
)
table(df1$gearlevel)
4 5
12 5
它可以工作,但是需要大量输入。有快速的方法吗?