read.transactions: Read Transaction Data

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

View source: R/read_write.R

Description

Reads a transaction data file from disk and creates a transactions object.

Usage

1
2
3
4
read.transactions(file, format = c("basket", "single"), sep = "",
                  cols = NULL, rm.duplicates = FALSE, 
                  quote = "\"'", skip = 0, 
                  encoding = "unknown")

Arguments

file

the file name.

format

a character string indicating the format of the data set. One of "basket" or "single", can be abbreviated.

sep

a character string specifying how fields are separated in the data file. The default ("") splits at whitespaces.

cols

For the ‘single’ format, cols is a numeric or character vector of length two giving the numbers or names of the columns (fields) with the transaction and item ids, respectively. If character, the first line of file is assumed to be a header with column names. For the ‘basket’ format, cols can be a numeric scalar giving the number of the column (field) with the transaction ids. If cols = NULL, the data do not contain transaction ids.

rm.duplicates

a logical value specifying if duplicate items should be removed from the transactions.

quote

a list of characters used as quotes when reading.

skip

number of lines to skip in the file before start reading data.

encoding

character string indicating the encoding which is passed to readLines or scan (see Encoding).

Details

For ‘basket’ format, each line in the transaction data file represents a transaction where the items (item labels) are separated by the characters specified by sep. For ‘single’ format, each line corresponds to a single item, containing at least ids for the transaction and the item.

Value

Returns an object of class transactions.

Author(s)

Michael Hahsler and Kurt Hornik

See Also

transactions-class

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## create a demo file using basket format for the example
data <- paste(
  "# this is some test data", 
  "item1, item2", 
  "item1", 
  "item2, item3", 
  sep="\n")
cat(data)
write(data, file = "demo_basket")

## read demo data (skip comment line)
tr <- read.transactions("demo_basket", format = "basket", sep=",", skip = 1)
inspect(tr)


## create a demo file using single format for the example
## column 1 contains the transaction ID and column 2 contains one item
data <- paste(
  "trans1 item1", 
  "trans2 item1",
  "trans2 item2", 
  sep ="\n")
cat(data)
write(data, file = "demo_single")

## read demo data
tr <- read.transactions("demo_single", format = "single", cols = c(1,2))
inspect(tr)


## tidy up
unlink("demo_basket")
unlink("demo_single")

Example output

Loading required package: Matrix

Attaching package: 'arules'

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

    abbreviate, write

# this is some test data
item1, item2
item1
item2, item3    items        
[1] {item1,item2}
[2] {item1}      
[3] {item2,item3}
trans1 item1
trans2 item1
trans2 item2    items         transactionID
[1] {item1}       trans1       
[2] {item1,item2} trans2       

arules documentation built on April 7, 2018, 9:03 a.m.