Skip to contents

Create a within-group percentage variable

Usage

group_perc(
  dat,
  project,
  id_group,
  group = NULL,
  value,
  name = "group_perc",
  create_group_ID = FALSE,
  drop_total_col = FALSE
)

Arguments

dat

Main data frame over which to apply function. Table in FishSET database should contain the string `MainDataTable`.

project

String, project name.

id_group

String, primary grouping variable(s). Used to create the "total_value" variable which sums value by id_group. If group = NULL, then value is divided by "total_value".

group

String, secondary grouping variable(s). Used to create the "group_total" variable which sums value by id_group and group. Percentage is calculated by dividing "group_total" by "total_value". Defaults to NULL.

value

String, the value variable used to calculate percentage. Must be numeric.

name

String, the name for the new variable. Defaults to "group_perc".

create_group_ID

Logical, whether to create a group ID variable using ID_var. Defaults to FALSE.

drop_total_col

Logical, whether to remove the "total_value" and "group_total" variables created to calculate percentage. Defaults to FALSE.

Details

group_perc creates a within-group percentage variable using a primary group ID (id_group) and secondary group (group). The total value of id_group is stored in the "total_value" variable, and the within-group total stored in "group_total". The group percentage is calculated using these two function-created variables. "total_value" and "group_total" can be dropped by setting drop_total_col = TRUE. A group ID column can be created using the variables inid_group and group by setting create_group_ID = TRUE.

Examples

if (FALSE) {
group_perc(pollockMainDataTable, "pollock", id_group = "PERMIT", group = NULL, 
           value = "OFFICIAL_TOTAL_CATCH_MT")
           
group_perc(pollockMainDataTable, "pollock", id_group = "PERMIT",
           group = "DISEMBARKED_PORT", value = "HAUL")
}