We can use dcast
from data.table
(See the Efficient reshaping using data.tables vignette on the project wiki or on the CRAN project page).
dcast(dt, opinion~party, value.var='ID', length)
Benchmarks
If we use a slightly bigger dataset and compare the speed using dcast
from reshape2
and data.table
set.seed(24)
df <- data.frame(ID=1:1e6, opinion=sample(letters, 1e6, replace=TRUE),
party= sample(1:9, 1e6, replace=TRUE))
system.time(dcast(df, opinion ~ party, value.var='ID', length))
# user system elapsed
# 0.278 0.013 0.293
system.time(dcast(setDT(df), opinion ~ party, value.var='ID', length))
# user system elapsed
# 0.022 0.000 0.023
system.time(setDT(df)[, .N, by = .(opinion, party)])
# user system elapsed
# 0.018 0.001 0.018
The third option is slightly better but it is in 'long' format. If the OP wants to have a 'wide' format, the data.table
dcast
can be used.
NOTE: I am using the the devel version i.e. v1.9.7
, but the CRAN should be fast enough.