Learn R Programming

Matrix.utils (version 0.5)

merge.Matrix: Merges two Matrices or matrix-like objects

Description

Similar to merge or join. Currently can only do left and inner joins that only find the first matching row; equivalent to plyr::join(match='first'). This is sufficient for N to 1 joins, such as joining a list of addresses to a zip code lookup table. Synonym for merge.Matrix.

Usage

## S3 method for class 'Matrix':
merge(x, y, by.x = rownames(x), by.y = rownames(y),
  type = "left", ...)

join.Matrix(x, y, by.x = rownames(x), by.y = rownames(y), type = "left",
  ...)

Arguments

x
Matrix or matrix-like object
y
Matrix or matrix-like object
by.x
vector indicating the names to match from Matrix x
by.y
vector indicating the names to match from Matrix y
type
type of join: currently on left and inner are supported
...
arguments to be passed to or from methods. Currently ignored

Examples

Run this code
orders<-Matrix(as.matrix(data.frame(orderNum=1:1000,
   customer=sample(100,1000,TRUE))),sparse=TRUE)
cancelledOrders<-Matrix(as.matrix(data.frame(orderNum=sample(1000,100),
   cancelled=1)),sparse=TRUE)
skus<-Matrix(as.matrix(data.frame(orderNum=sample(1000,10000,TRUE),
   sku=sample(1000,10000,TRUE),
   amount=runif(10000))),sparse=TRUE)
a<-merge.Matrix(orders,cancelledOrders,orders[,'orderNum'],cancelledOrders[,'orderNum'])
b<-merge.Matrix(orders,cancelledOrders,orders[,'orderNum'],cancelledOrders[,'orderNum'],
   type='inner')
c<-merge.Matrix(orders,skus,orders[,'orderNum'],skus[,'orderNum'])

## Not run:
orders<-data.frame(orderNum=sample(1e6, 1e7, TRUE),
   sku=sample(1e3, 1e7, TRUE),
   customer=sample(1e4,1e7,TRUE))
cancelledOrders<-data.frame(data.frame(orderNum=sample(1e6,1e5),cancelled=1))
system.time(b<-merge.Matrix(orders,cancelledOrders,orders[,'orderNum'],
   cancelledOrders[,'orderNum'],type='inner'))
#The following is the equivalent call in plyr, but returns an error due to a bug in plyr
system.time(c<-plyr::join(orders,cancelledOrders,type='inner',match='first'))

Run the code above in your browser using DataLab