> mm1 <- cast(mm, Class~Name) > aba <- mm1[,2:6] > aba[is.na(aba)] <- 1 > final <- data.frame(mm1$Class, aba) > names(final) <- names(mm1) > final You can abbreviate all this to: final <- cast(mm, Class ~ Name, fill = 1) Hadley -- http://had.co.nz/