Summarizing data tables

Here we use data from the British Election Study 2010. The data set bes2010feelings.RData is prepared from the original available at https://www.britishelectionstudy.com/data-object/2010-bes-cross-section/ by removing identifying information and scrambling the data.

load("bes2010feelings.RData")
library(data.table)
setDT(bes2010feelings)
Mean <- function(x) mean(x,na.rm=TRUE)
bes2010feelings[,.(Brown=Mean(flng.brown),
                   Cameron=Mean(flng.cameron),
                   Clegg=Mean(flng.clegg),
                   N=.N)]
  Brown    Cameron  Clegg    N   
1 4.406517 5.162484 4.850231 5010
bes2010feelings[,.(Brown=Mean(flng.brown),
                   Cameron=Mean(flng.cameron),
                   Clegg=Mean(flng.clegg),
                   N=.N),
                by=.(wave,region)]
  wave region   Brown    Cameron  Clegg    N   
1 Pre  England  4.092674 5.284810 4.618690 1159
2 Pre  NA       4.507143 4.929870 4.426573  437
3 Pre  Scotland 5.395000 4.502591 4.405229  207
4 Pre  Wales    4.328244 4.774194 4.592233  132
5 Post England  4.140990 5.441454 5.160313 2175
6 Post Scotland 5.510769 4.539075 4.513793  665
7 Post Wales    4.307692 4.855895 4.814480  235