Skip to contents

Recode Numeric Variables to Factor Variables

Usage

recodeCat(df, scale_labels)

Arguments

df

Required, a tibble/data frame of survey items that are numeric variables that need to be converted into factor variables, Can be anywhere from 3 to 7 point scales.

scale_labels

Required, a named character vector of labels of the desired scale levels for the new factor variables. The function will use this vector to convert the numeric variables into factor variables, all levels must be supplied in the correct range otherwise else NA will be returned for variables outside the range of user supplied values. The named character vector should have the new labels as the "name" and the old labels as the "variable" like this: c("" = "") which would look like this: levels_min_ext <- c("Minimal" = "1", "Slight" = "2", "Moderate" = "3", "Good" = "4", "Extensive" = "5")

Value

a tibble with the original numeric variables along with new variables that are now factors with the prefix "cat_variable_name", with levels taken from scale_labels character vector.

Examples

items <- dplyr::tibble(
  pre_Organization = c(1, 2, 3, 4, 5, 4, 3, 2, 1),
  post_Organization = dplyr::if_else(pre_Organization < 5, pre_Organization + 1, pre_Organization),
  pre_Source = c(2, 2, 3, 5, 4, 3, 2, 1, 2),
  post_Source = dplyr::if_else(pre_Source < 4, pre_Source + 2, pre_Source),
  pre_Publish = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
  post_Publish = pre_Publish + 2,
  pre_Write = c(2, 2, 2, 3, 3, 3, 4, 4, 4),
  post_Write = pre_Write + 1,
  pre_Research = c(1, 1, 2, 2, 3, 3, 4, 4, 4),
  post_Research = pre_Research + 1
)

levels_min_ext <- c("Minimal" = "1", "Slight" = "2", "Moderate" = "3",
                     "Good" = "4", "Extensive" = "5")

recodeCat(df = items, scale_labels = levels_min_ext)
#> # A tibble: 9 × 20
#>   pre_Organization post_Organization pre_Source post_Source pre_Publish
#>              <dbl>             <dbl>      <dbl>       <dbl>       <dbl>
#> 1                1                 2          2           4           1
#> 2                2                 3          2           4           1
#> 3                3                 4          3           5           1
#> 4                4                 5          5           5           2
#> 5                5                 5          4           4           2
#> 6                4                 5          3           5           2
#> 7                3                 4          2           4           3
#> 8                2                 3          1           3           3
#> 9                1                 2          2           4           3
#> # ℹ 15 more variables: post_Publish <dbl>, pre_Write <dbl>, post_Write <dbl>,
#> #   pre_Research <dbl>, post_Research <dbl>, cat_pre_Organization <fct>,
#> #   cat_post_Organization <fct>, cat_pre_Source <fct>, cat_post_Source <fct>,
#> #   cat_pre_Publish <fct>, cat_post_Publish <fct>, cat_pre_Write <fct>,
#> #   cat_post_Write <fct>, cat_pre_Research <fct>, cat_post_Research <fct>