首页 > 解决方案 > 在R中打印有序表

问题描述

使用 df 像:

df <- tibble(dist = c(x,x,y,x,y), desc = c("txt","txt2","txt3","txt4,"txt5"), count = c(20,10,5,30,10))

如何打印一张表格dist,按以下顺序排列count

desc   count
txt4   30
txt1   20
txt2   10

标签: r

解决方案


使用dplyr

library(dplyr)

df %>% filter(dist == 'x') %>% arrange(desc(count)) %>% select(-dist)

#  desc  count
#  <chr> <dbl>
#1 txt4     30
#2 txt1     20
#3 txt2     10

或在基础 R 中:

temp <- subset(df, dist == 'x', select = -dist)
temp[order(-temp$count), ]

推荐阅读