天天看点

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
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 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
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 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
           

继续阅读