sort: Sort Associations

Description Usage Arguments Details Value Author(s) See Also Examples

Description

Provides the method sort to sort elements in class associations (e.g., itemsets or rules) according to the value of measures stored in the association's slot quality (e.g., support).

Usage

1
2
3
4
5
6
7
8
## S4 method for signature 'associations'
sort(x, decreasing = TRUE, na.last = NA, 
    by = "support", order = FALSE, ...)

## S4 method for signature 'associations'
head(x, n = 6L, by = NULL, decreasing = TRUE, ...)
## S4 method for signature 'associations'
tail(x, n = 6L, by = NULL, decreasing = TRUE, ...)

Arguments

x

an object to be sorted.

decreasing

a logical. Should the sort be increasing or decreasing? (default is decreasing)

na.last

na.last is not supported for associations. NAs are always put last.

by

a character string specifying the quality measure stored in x to be used to sort x. If a vector of character strings is specified then the additional strings are used to sort x in case of ties.

order

should a order vector be returned instead of the sorted associations?

n

a single integer indicating the number of associations returned.

...

Further arguments are ignored.

Details

sort is relatively slow for large sets of associations since it has to copy and rearrange a large data structure. Note that sorting creates a second copy of the set of associations which can be slow and memory consuming for large sets. With order = TRUE a integer vector with the order is returned instead of the reordered associations.

If only the top n associations are needed then head using by performs this faster than calling sort and then head since it does it without copying and rearranging all the data. tail works in the same way.

Value

An object of the same class as x.

Author(s)

Michael Hahsler

See Also

associations-class

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
data("Adult")

## Mine rules with APRIORI
rules <- apriori(Adult, parameter = list(supp = 0.6))

rules_by_lift <- sort(rules, by = "lift")

inspect(head(rules))
inspect(head(rules_by_lift))

## A faster/less memory consuming way to get the top 5 rules according to lift 
## (see Details section)
inspect(head(rules, n = 5, by = "lift"))

Example output

Loading required package: Matrix

Attaching package: 'arules'

The following objects are masked from 'package:base':

    abbreviate, write

Apriori

Parameter specification:
 confidence minval smax arem  aval originalSupport maxtime support minlen
        0.8    0.1    1 none FALSE            TRUE       5     0.6      1
 maxlen target   ext
     10  rules FALSE

Algorithmic control:
 filter tree heap memopt load sort verbose
    0.1 TRUE TRUE  FALSE TRUE    2    TRUE

Absolute minimum support count: 29305 

set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[115 item(s), 48842 transaction(s)] done [0.03s].
sorting and recoding items ... [6 item(s)] done [0.00s].
creating transaction tree ... done [0.02s].
checking subsets of size 1 2 3 4 done [0.00s].
writing ... [39 rule(s)] done [0.00s].
creating S4 object  ... done [0.00s].
    lhs           rhs                            support   confidence lift     
[1] {}         => {race=White}                   0.8550428 0.8550428  1.0000000
[2] {}         => {native-country=United-States} 0.8974243 0.8974243  1.0000000
[3] {}         => {capital-gain=None}            0.9173867 0.9173867  1.0000000
[4] {}         => {capital-loss=None}            0.9532779 0.9532779  1.0000000
[5] {sex=Male} => {capital-gain=None}            0.6050735 0.9051455  0.9866565
[6] {sex=Male} => {capital-loss=None}            0.6331027 0.9470750  0.9934931
    count
[1] 41762
[2] 43832
[3] 44807
[4] 46560
[5] 29553
[6] 30922
    lhs                               rhs                              support confidence     lift count
[1] {race=White}                   => {native-country=United-States} 0.7881127  0.9217231 1.027076 38493
[2] {native-country=United-States} => {race=White}                   0.7881127  0.8781940 1.027076 38493
[3] {race=White,                                                                                        
     capital-loss=None}            => {native-country=United-States} 0.7490480  0.9205626 1.025783 36585
[4] {race=White,                                                                                        
     capital-gain=None}            => {native-country=United-States} 0.7194628  0.9202807 1.025469 35140
[5] {capital-loss=None,                                                                                 
     native-country=United-States} => {race=White}                   0.7490480  0.8762454 1.024797 36585
[6] {race=White,                                                                                        
     capital-gain=None,                                                                                 
     capital-loss=None}            => {native-country=United-States} 0.6803980  0.9189249 1.023958 33232
    lhs                               rhs                              support confidence     lift count
[1] {race=White}                   => {native-country=United-States} 0.7881127  0.9217231 1.027076 38493
[2] {native-country=United-States} => {race=White}                   0.7881127  0.8781940 1.027076 38493
[3] {race=White,                                                                                        
     capital-loss=None}            => {native-country=United-States} 0.7490480  0.9205626 1.025783 36585
[4] {race=White,                                                                                        
     capital-gain=None}            => {native-country=United-States} 0.7194628  0.9202807 1.025469 35140
[5] {capital-loss=None,                                                                                 
     native-country=United-States} => {race=White}                   0.7490480  0.8762454 1.024797 36585

arules documentation built on Nov. 17, 2017, 6:02 a.m.