write: Write Transactions or Associations to a File

Description Usage Arguments Details Author(s) See Also Examples

Description

Provides the generic function write and the S4 methods to write transactions or associations (itemsets, rules) to a file.

Usage

1
2
3
4
5
6
write(x, file = "",...)
## S4 method for signature 'transactions'
write(x, file="", format = c("basket", "single"),
                sep=" ", quote=TRUE, ...)
## S4 method for signature 'associations'
write(x, file="", sep=" ", quote=TRUE, ...)

Arguments

x

the transactions or associations (rules, itemsets, etc.) object.

file

either a character string naming a file or a connection open for writing. '""' indicates output to the console.

format

format to write transactions.

sep

the field separator string. Values within each row of x are separated by this string. Use quote=TRUE and sep="," for saving data as in csv format.

quote

a logical value. Quote fields?

...

further arguments passed on to write.table or write. Use fileEncoding to set the encoding used for writing the file.

Details

For associations (rules and itemsets) write first uses coercion to data.frame to obtain a printable form of x and then uses write.table to write the data to disk.

Transactions can be saved in basket (one line per transaction) or in single (one line per item) format.

Note: To save and load associations in compact form, use save and load from the base package. Alternatively, association can be written to disk in PMML (Predictive Model Markup Language) via write.PMML. This requiresvpackages pmml.

Author(s)

Michael Hahsler

See Also

read.transactions for reading transactions from a file, read.PMML and write.PMML for reading/writing associations in PMML format, write.table (in base), transactions-class, associations-class

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
data("Epub")

## write the formated transactions to screen (basket format)
write(head(Epub))

## write the formated transactions to screen (single format)
write(head(Epub), format="single")

## write the formated result to file in CSV format
write(Epub, file = "data.csv", format="single", sep = ",")

## write rules in CSV format
rules <- apriori(Epub, parameter=list(support=0.0005, conf=0.8))
write(rules, file = "data.csv", sep = ",")

unlink("data.csv") # tidy up

Example output

Loading required package: Matrix

Attaching package: 'arules'

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

    abbreviate, write

"doc_154"
"doc_3d6"
"doc_16f"
"doc_11d" "doc_1a7" "doc_f4"
"doc_83"
"doc_11d"
"session_4795" "doc_154"
"session_4797" "doc_3d6"
"session_479a" "doc_16f"
"session_47b7" "doc_11d"
"session_47b7" "doc_1a7"
"session_47b7" "doc_f4"
"session_47bb" "doc_83"
"session_47c2" "doc_11d"
Apriori

Parameter specification:
 confidence minval smax arem  aval originalSupport maxtime support minlen
        0.8    0.1    1 none FALSE            TRUE       5   5e-04      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: 7 

set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[936 item(s), 15729 transaction(s)] done [0.00s].
sorting and recoding items ... [687 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 3 4 5 6 done [0.00s].
writing ... [374 rule(s)] done [0.00s].
creating S4 object  ... done [0.00s].

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