i have data.frame looks this. x 1 x b 2 x c 3 y 3 y b 3 y c 2 i want in matrix form can feed heatmap make plot. result should like: b c x 1 2 3 y 3 3 2 i have tried cast reshape package , have tried writing manual function not seem able right. there many ways this. answer starts favorite ways, collects various ways answers similar questions scattered around site. tmp <- data.frame(x=gl(2,3, labels=letters[24:25]), y=gl(3,1,6, labels=letters[1:3]), z=c(1,2,3,3,3,2)) using reshape2: library(reshape2) acast(tmp, x~y, value.var="z") using matrix indexing: with(tmp, { out <- matrix(nrow=nlevels(x), ncol=nlevels(y), dimnames=list(levels(x), levels(y))) out[cbind(x, y)] <- z out }) using xtabs : xtabs(z~x+y, data=tmp) you can use reshape , suggested here: convert table matrix column names , though have little manipulation afterwards remove colu