FiofanS FiofanS - 1 month ago 6
R Question

How to count aggregated data and create different counters?

I have a data frame

df
with different columns.

df = data.frame(c("2012","2012","2012","2013"),
c("AAA","BBB","AAA","AAA"),
c("X","Not-serviced","X","Y"))
colnames(df) = c("year","type","service_type")


I need to get the following dataframe
df2
:

year type num_serviced num_notserviced num_total
2012 AAA 2 0 2
...


So, I need to group data by
type
and
year
, and then to count the frequency of
Not-serviced
and the all others, e.g.
X
,
Y
, etc. (supposed as Serviced).

This is my code that calculates Total:

temp = aggregate(df,
list(type = dat_human_errors$type,
year = dat_human_errors$year),
FUN = function(x){NROW(x)})


However how to create
num_serviced
and
num_notserviced
? There should be some IF-THEN rule like
if type=="Not-serviced" num_notserviced++ else num_serviced++
.

Answer

We can try with data.table. Convert the 'data.frame' to 'data.table' (setDT(df)), grouped by 'year', 'type', get the sum of logical vectors, and finally get the total.

library(data.table)
setDT(df)[, .(num_serviced = sum(service_type != "Not-serviced"), 
      num_notserviced = sum(service_type =="Not_serviced")), 
     .(year, type)][, Total := num_serviced + num_notserviced][]
Comments