How to sum a variable by group

Using aggregate: aggregate(x$Frequency, by=list(Category=x$Category), FUN=sum) Category x 1 First 30 2 Second 5 3 Third 34 In the example above, multiple dimensions can be specified in the list. Multiple aggregated metrics of the same data type can be incorporated via cbind: aggregate(cbind(x$Frequency, x$Metric2, x$Metric3) … (embedding @thelatemail comment), aggregate has a formula interface too aggregate(Frequency … Read more

R transpose and group by

This can be achieved using dcast() from the data.table package: library(data.table) dcast(DT, Client ~ rowid(Client, prefix = “Smoke “), value.var = “Smoke”) # Client Smoke 1 Smoke 2 Smoke 3 #1: 21 Y Y Y Data DT <- fread(“Client Smoke 0021 Y 0021 Y 0021 Y”)