Sum values in numeric column sorted by variable groupings.
summed_output.RdThis function sums column of numeric type using dplyr pipeline. Users can set grouping hierarchies and filter conditions to specify calculations.
Arguments
- data
A dataframe that the function will be applied to.
- group_vars
variable(s)/column name(s) of a dataframe that is used to group the observations based on common characteristics. "group_vars" specifies the type of operation (group_by). Set to "NULL" as default.
- sum_var
a character string that represents a column name/variable containing numeric values. "sum_var" specifies the type of operation (sum) for the variable.
- filter_vars
variable(s)/column(s) called on to set conditions for filtering. "filter_vars" specifies the type of operation (filter) for the selected variables. Set to TRUE as default so user can select filter conditions or leave the argument unused.
- na.rm
argument specifying how to handle NA values in data when performing sum operation; set to "TRUE" as default to prevent an NA output in the sum operation when the column is passed through the function.
Value
A dataframe with a new column named "cumulative" containing the summed output value for each grouping.
Examples
# Using the function without any grouping or filtering conditions
summed_output(data = palmerpenguins::penguins,
sum_var = "body_mass_g")
#> # A tibble: 1 × 1
#> cumulative
#> <int>
#> 1 1437000
# Applying filters by storing filter conditions as an object in the function call
sex_male_year_2007 <- palmerpenguins::penguins$sex == "male" & palmerpenguins::penguins$year == 2007
summed_output(data = palmerpenguins::penguins,
group_vars = c("island", "species"),
sum_var = "bill_depth_mm",
filter_vars = sex_male_year_2007,
na.rm = TRUE)
#> # A tibble: 5 × 3
#> # Groups: island [3]
#> island species cumulative
#> <fct> <fct> <dbl>
#> 1 Biscoe Adelie 91.5
#> 2 Biscoe Gentoo 261.
#> 3 Dream Adelie 194.
#> 4 Dream Chinstrap 249.
#> 5 Torgersen Adelie 143.