I have a dataframe df:
colour shape
\'red\' circle
\'blue\' square
\'blue\' circle
\'green\' sphere
And a double matrix m with named rows/
Another answer Using the reshape2 and plyr (optional just for join) packages.
require(plyr)
require(reshape2)
Df <- data.frame(colour = c("red", "blue", "blue", "green"),
shape = c("circle", "square", "circle", "sphere"))
Mat <- matrix(1:9, dimnames = list(c("red", "blue", "green"),
c("circle", "square", "sphere")),
nrow = 3)
Df2 <- melt.array(Mat, varnames = c("colour", "shape"))
join(Df, Df2)
result <- join(Df, Df2)
join(Df, Df2)
Joining by: colour, shape
colour shape value
1 red circle 1
2 blue square 5
3 blue circle 2
4 green sphere 9
Hope this help