Skip to contents

This function makes it easy to calculate row means for multiple variables. It uses <tidy-select> syntax to determine which variables to include in the operation.

Usage

row_means(cols, label = NULL, na.rm = TRUE)

Arguments

cols

<tidy-select> The variables you want to use when calculating row means

label

A string specifying the variable label. If not specified, defaults to NULL

na.rm

Determines if NAs should be removed. Default is TRUE.

Details

This function also has the option of adding a new variable label attribute. Furthermore, it automatically adds two more attributes: transformation and variables. The trasnformation attribute basically explains how the variable was created by saying "Took the average of..." and then lists the variables included. variables just lists the variables included in the operation.

Examples

# load the dplyr package
library(dplyr)
# make a new df with the new column
new <- test_data %>% 
  mutate(
    sdo_avg_new = row_means(
      # specify the variables involved in the row means
      cols = c(top_rev:deserving_flip),
      # specify the variable label
      label = "Social Dominance Orientation Average",
      # remove NAs
      na.rm = TRUE
    )
  )
#> Error in mutate(., sdo_avg_new = row_means(cols = c(top_rev:deserving_flip),     label = "Social Dominance Orientation Average", na.rm = TRUE)):  In argument: `sdo_avg_new = row_means(...)`.
#> Caused by error in `row_means()`:
#> ! could not find function "row_means"

# Show that the attributes
attributes(new$sdo_avg_new)
#> Error in new$sdo_avg_new: object of type 'closure' is not subsettable

# show the output
new$sdo_avg_new
#> Error in new$sdo_avg_new: object of type 'closure' is not subsettable