简体   繁体   English

使用 dplyr 过滤、表格和排序?

[英]Filter, Table, and Sort with dplyr?

I'm trying to filter a variable by removing NA's, then table the variable, and then sort by descending.我试图通过删除 NA 来过滤变量,然后对变量进行表列,然后按降序排序。 I've tried the following我试过以下

library(dplyr)
df %>% filter(!is.na(var)) %>% data.frame(sort(table(var),decreasing=TRUE))

Any idea how to get this to work?知道如何让它发挥作用吗?

The pipe passes the result of the function call on the left as the first argument to the function on the right.管道将左侧函数调用的结果作为第一个参数传递给右侧函数。 Here you've tried to explicitly supply the argument to data.frame instead, so it will error.在这里,您已尝试将参数显式提供给data.frame ,因此它会出错。 Instead, try this:相反,试试这个:

df %>%
  filter(!is.na(var)) %>%
  count(var) %>%
  arrange(desc(n))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM