In R programming, function aggregate() provides an easy way to calculate summary statistics of variables by specific groups in a…