How to find % of cases based on group by on multiple columns

r
group_by

#1

Hello,

In the following lines of code,I am trying to find the % of fraudulent cases in each sub-group,like % of fraudulent cases within unmarried females as a percentage of unmarried females.Similarly for other groups like married females,married males & unmarried males.

insurance_males <- subset(insurance,gender == 1)
insurance_males_unmarried <- subset(insurance_males,marital == 0)
insurance_males_married <- subset(insurance_males,marital == 1)

insurance_female <- subset(insurance,gender == 0)
insurance_female_unmarried <- subset(insurance_female,marital == 0)
insurance_female_married <- subset(insurance_female,marital == 1)

table(insurance_males_unmarried$fraudulent)/nrow(insurance_males_unmarried)
table(insurance_males_married$fraudulent)/nrow(insurance_males_married)
table(insurance_female_unmarried$fraudulent)/nrow(insurance_female_unmarried)
table(insurance_female_married$fraudulent)/nrow(insurance_female_married)

This is the output I am getting:

I wanted to know if there is shorter method to do this.I can use:

library(data.table)
DT <- data.table(DF)
DT[, Mean:=mean(X), by=list(Y, Z)]

to do group by on multiple groups but how do I find the proportions based on each subgroup??
Can somebody please help me with this??