Alternative approach using spread from tidyr :
library(tidyr) spread(dt, SAMPLE, count, fill=0)
Or an old school solution with reshape from stats :
reshape(dt, timevar='SAMPLE', idvar=c('junction'), direction='wide')
Data:
dt = structure(list(SAMPLE = c("R1", "R2", "R3", "R3", "R1"), junction = c("a", "a", "b", "a", "c"), count = c(1, 1, 1, 1, 2)), .Names = c("SAMPLE", "junction", "count"), row.names = c(NA, -5L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x05e924a0>)
source share