I have data for several years for different locations. I want to get a count by year, by location:
library(data.table)
t1 <- data.table(ID = 1:100, Year = 2010:2015, Loc = LETTERS[1:7])
t1 <- t1[, .N, by=.(Year, Loc)]
# Year Type N
# 1: 2010 A 3
# 2: 2011 B 3
# 3: 2012 C 3
# ...
However, what I want is a data.table solution similar to the one below using dplyr:
library(dplyr)
spread(t1, Loc, N)
# Year A B C D E F G
# 1: 2010 3 2 2 2 2 3 3
# ...
How can I do this using data.table? I have millions of rows across ~100 variables many of which have thousands of factor levels, and I'd prefer to stick to data.table. I tried a number of lapply solutions to other group-by-two-variable questions, but they all threw the error .N is not a function? Am I missing something really obvious?