栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > R语言

R语言 排序问题

R语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

R语言 排序问题

解决R语言排序问题的方法:

  • order from base
  • arrange from dplyr
  • setorder and setorderv from data.table
  • arrange from plyr
  • sort from taRifx
  • orderBy from doBy
  • sortData from Deducer
  • Most of the time you should use the dplyr or data.table solutions, unless having no-dependencies is important, in which case use base::order.
plyr::arrange(dd, desc(z), b)     ## plyr
arrange(dd, desc(z), b)           ## dplyr
sort(dd, f = ~ -z + b)            ## taRifx
dd[with(dd, order(-z, b)), ]     ## base R
library(dplyr)
library(data.table)
df1 <- tbl_df(iris)
#using strings or formula
arrange_(df1, c('Petal.Length', 'Petal.Width'))
arrange_(df1, ~Petal.Length, ~Petal.Width)
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
                                   
 1         4.60        3.60         1.00       0.200 setosa 
 2         4.30        3.00         1.10       0.100 setosa 
 3         5.80        4.00         1.20       0.200 setosa 
 4         5.00        3.20         1.20       0.200 setosa 
 5         4.70        3.20         1.30       0.200 setosa 
 6         5.50        3.50         1.30       0.200 setosa 
 7         4.40        3.00         1.30       0.200 setosa 
 8         4.40        3.20         1.30       0.200 setosa 
 9         5.00        3.50         1.30       0.300 setosa 
10         4.50        2.30         1.30       0.300 setosa 
# ... with 140 more rows
#Or using a variable
sortBy <- c('Petal.Length', 'Petal.Width')
arrange_(df1, .dots = sortBy)

# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
                                   
 1         4.60        3.60         1.00       0.200 setosa 
 2         4.30        3.00         1.10       0.100 setosa 
 3         5.80        4.00         1.20       0.200 setosa 
 4         5.00        3.20         1.20       0.200 setosa 
 5         4.70        3.20         1.30       0.200 setosa 
 6         5.50        3.50         1.30       0.200 setosa 
 7         4.40        3.00         1.30       0.200 setosa 
 8         4.40        3.20         1.30       0.200 setosa 
 9         5.00        3.50         1.30       0.300 setosa 
10         4.50        2.30         1.30       0.300 setosa 
# ... with 140 more rows
  • 其次是sort()功能

#Doing the same operation except sorting Petal.Length in descending order
sortByDesc <- c('desc(Petal.Length)', 'Petal.Width')
arrange_(df1, .dots = sortByDesc)



dt1 <- data.table(iris) #not really required, as you can work directly on your data.frame
sortBy <- c('Petal.Length', 'Petal.Width')
sortType <- c(-1, 1)
setorderv(dt1, sortBy, sortType)
dt1


 Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
  1:          7.7         2.6          6.9         2.3 virginica
  2:          7.7         2.8          6.7         2.0 virginica
  3:          7.7         3.8          6.7         2.2 virginica
  4:          7.6         3.0          6.6         2.1 virginica
  5:          7.9         3.8          6.4         2.0 virginica
 ---                                                            
146:          5.4         3.9          1.3         0.4    setosa
147:          5.8         4.0          1.2         0.2    setosa
148:          5.0         3.2          1.2         0.2    setosa
149:          4.3         3.0          1.1         0.1    setosa
150:          4.6         3.6          1.0         0.2    setosa



dd <- data.frame(b = factor(c("Hi", "Med", "Hi", "Low"), 
                            levels = c("Low", "Med", "Hi"), ordered = TRUE),
                 x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9),
                 z = c(1, 1, 1, 2))
library(taRifx)
dd1 <- sort(dd, f= ~ -z + b )

> dd
    b x y z
1  Hi A 8 1
2 Med D 3 1
3  Hi A 9 1
4 Low C 9 2
> dd1
    b x y z
4 Low C 9 2
2 Med D 3 1
1  Hi A 8 1
3  Hi A 9 1
doBy()
dd <- data.frame(b = factor(c("Hi", "Med", "Hi", "Low"), 
                            levels = c("Low", "Med", "Hi"), ordered = TRUE),
                 x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9),
                 z = c(1, 1, 1, 2))

library(doBy)
dd
orderBy( ~ -z + b, data = dd)     ## doBy
> dd
    b x y z
1  Hi A 8 1
2 Med D 3 1
3  Hi A 9 1
4 Low C 9 2
> orderBy( ~ -z + b, data = dd)     ## doBy
    b x y z
4 Low C 9 2
2 Med D 3 1
1  Hi A 8 1
3  Hi A 9 1
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/855754.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号