calculate_nps: Quickly extract the Net Promoter Score (NPS) from a...

View source: R/calculate_nps.R

calculate_npsR Documentation

Quickly extract the Net Promoter Score (NPS) from a frequencies object of formatted NPS survey questions

Description

Quickly extract the Net Promoter Score (NPS) from a frequencies object of formatted NPS survey questions

Usage

calculate_nps(
  frequencies,
  result = result,
  label = label,
  value = value,
  input_type = c("grouped", "numeric"),
  by_variable = TRUE,
  variable = variable,
  add_group = TRUE,
  get_brand = TRUE,
  prompt = prompt,
  brand_var_name = "brand",
  prompt_rm_pre = ".+\\,.+recommend ",
  prompt_rm_post = " to a .+\\? \\-.+",
  arrange_nps = TRUE,
  append_nps_to_brand = TRUE,
  brand_factor = TRUE
)

Arguments

frequencies

A frequencies table as produced by y2clerk::freqs()

result

DEFAULT = result; The column of NPS question results (proportions) used to calculate the final NPS

label

DEFAULT = label; The column of NPS question labels (responses) used to calculate the final NPS (if 'input_type' is set to "grouped", this column MUST include the values "Detractor", "Passive", and "Promoter" and no other values)

value

DEFAULT = value; The column of NPS question values (levels) used to order the final output

input_type

DEFAULT = "grouped"; The input type of the NPS questions. Must be one of either "grouped" or "numeric" corresponding to either pre-formatted or unformatted Qualtrics NPS questions, respectively

by_variable

DEFAULT = TRUE; Boolean, controls whether the function calculates the NPS for each unique specified 'variable' (question columns) or for the frequencies as a whole

variable

DEFAULT = variable; The column of unique NPS questions and question names used to calculate the NPS for each (if 'by_variable' is set to TRUE)

add_group

DEFAULT = TRUE; Boolean, controls whether the function adds the specified 'variable' as a grouping variable to the frequencies object (if it is already grouped) or if the specified 'variable' will overwrite any and all previously applied grouping variables

get_brand

DEFAULT = TRUE; Boolean, controls whether the function extracts the "brand" (NPS question subject) from the specified 'prompt' column or does not

prompt

DEFAULT = prompt; The column of NPS question prompts (question texts) from which to extract the "brand" (if 'get_brand' is set to TRUE)

brand_var_name

DEFAULT = "brand"; The name assigned to the output "brand" column (if 'get_brand' is set to TRUE)

prompt_rm_pre

DEFAULT = ".+\,.+recommend "; String pattern in the specified 'prompt' column before which everything in the column is scrubbed to obtain the "brand"

prompt_rm_post

DEFAULT = " to a .+\? \-.+"; String pattern in the specified 'prompt' column after which everything in the column is scrubbed to obtain the "brand"

arrange_nps

DEFAULT = TRUE; Boolean, whether to arrange the final output by the NPS results, with previous frequencies arrangements/orderings still intact

append_nps_to_brand

DEFAULT = TRUE; Boolean, whether to append the NPS values to the "brand" column, having the format "["brand"] (NPS = [NPS])"

brand_factor

DEFAULT = TRUE; Boolean, whether to convert the "brand" variable (with appended NPS values) to a factor for ease of data visualization (argument specification only applied if 'append_nps_to_brand' is set to TRUE)

Value

An updated frequencies object with the new NPS column (and other specified columns) attached, formatted as specified

Examples


set.seed(1)

df <- data.frame(
  brand1_NPS_GROUP = sample(
    c(1:3, NA),
    size = 200,
    replace = TRUE
  ),
  brand2_NPS_GROUP = sample(
    c(1:3, NA),
    size = 200,
    replace = TRUE
  ),
  brand3_NPS_GROUP = sample(
    c(1:3, NA),
    size = 200,
    replace = TRUE
  )
) %>%
  labelled::set_value_labels(
    brand1_NPS_GROUP = c(
      'Promoter' = 3,
      'Passive' = 2,
      'Detractor' = 1
    ),
    brand2_NPS_GROUP = c(
      'Promoter' = 3,
      'Passive' = 2,
      'Detractor' = 1
    ),
    brand3_NPS_GROUP = c(
      'Promoter' = 3,
      'Passive' = 2,
      'Detractor' = 1
    )
  ) %>%
  labelled::set_variable_labels(
    brand1_NPS_GROUP = "How likely are you to recommend Brand1 to a friend or colleague? - Group",
    brand2_NPS_GROUP = "How likely are you to recommend Brand2 to a friend or colleague? - Group",
    brand3_NPS_GROUP = "How likely are you to recommend Brand3 to a friend or colleague? - Group"
  ) %>%
  tidyr::as_tibble()

# Frequencies
frequencies <- df %>%
  freqs(
    brand1_NPS_GROUP,
    brand2_NPS_GROUP,
    brand3_NPS_GROUP,
    nas = FALSE,
    prompt = TRUE
  )

# Calculate NPS
calculate_nps(frequencies)


y2analytics/y2clerk documentation built on Feb. 28, 2025, 5:47 p.m.