Changes the reference level of (numeric) factor.

ref_lvl(x, ..., lvl = NULL)

Arguments

x

A vector or data frame.

...

Optional, unquoted names of variables that should be selected for further processing. Required, if x is a data frame (and no vector) and only selected variables from x should be processed. You may also use functions like : or tidyselect's select-helpers. See 'Examples' or package-vignette.

lvl

Either numeric, indicating the new reference level, or a string, indicating the value label from the new reference level. If x is a factor with non-numeric factor levels, relevel(x, ref = lvl) is returned. See 'Examples'.

Value

x with new reference level. If x is a data frame, the complete data frame x will be returned, where variables specified in ... will be re-leveled; if ... is not specified, applies to all variables in the data frame.

Details

Unlike relevel, this function behaves differently for factor with numeric factor levels or for labelled data, i.e. factors with value labels for the values. ref_lvl() changes the reference level by recoding the factor's values using the rec function. Hence, all values from lowest up to the reference level indicated by lvl are recoded, with lvl starting as lowest factor value. For factors with non-numeric factor levels, the function simply returns relevel(x, ref = lvl). See 'Examples'.

See also

to_factor to convert numeric vectors into factors; rec to recode variables.

Examples

data(efc)
x <- to_factor(efc$e42dep)
str(x)
#>  Factor w/ 4 levels "1","2","3","4": 3 3 3 4 4 4 4 4 4 4 ...
#>  - attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"
#>  - attr(*, "label")= chr "elder's dependency"
frq(x)
#> elder's dependency (x) <categorical> 
#> # total N=908 valid N=901 mean=2.94 sd=0.94
#> 
#> Value |                Label |   N | Raw % | Valid % | Cum. %
#> -------------------------------------------------------------
#>     1 |          independent |  66 |  7.27 |    7.33 |   7.33
#>     2 |   slightly dependent | 225 | 24.78 |   24.97 |  32.30
#>     3 | moderately dependent | 306 | 33.70 |   33.96 |  66.26
#>     4 |   severely dependent | 304 | 33.48 |   33.74 | 100.00
#>  <NA> |                 <NA> |   7 |  0.77 |    <NA> |   <NA>

# see column "val" in frq()-output, which indicates
# how values/labels were recoded after using ref_lvl()
x <- ref_lvl(x, lvl = 3)
str(x)
#>  Factor w/ 4 levels "1","2","3","4": 1 1 1 4 4 4 4 4 4 4 ...
#>  - attr(*, "label")= chr "elder's dependency"
#>  - attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   ..- attr(*, "names")= chr [1:4] "moderately dependent" "independent" "slightly dependent" "severely dependent"
frq(x)
#> elder's dependency (x) <categorical> 
#> # total N=908 valid N=901 mean=2.58 sd=1.26
#> 
#> Value |                Label |   N | Raw % | Valid % | Cum. %
#> -------------------------------------------------------------
#>     1 | moderately dependent | 306 | 33.70 |   33.96 |  33.96
#>     2 |          independent |  66 |  7.27 |    7.33 |  41.29
#>     3 |   slightly dependent | 225 | 24.78 |   24.97 |  66.26
#>     4 |   severely dependent | 304 | 33.48 |   33.74 | 100.00
#>  <NA> |                 <NA> |   7 |  0.77 |    <NA> |   <NA>

library(dplyr)
dat <- efc %>%
  select(c82cop1, c83cop2, c84cop3) %>%
  to_factor()

frq(dat)
#> do you feel you cope well as caregiver? (c82cop1) <categorical> 
#> # total N=908 valid N=901 mean=3.12 sd=0.58
#> 
#> Value |     Label |   N | Raw % | Valid % | Cum. %
#> --------------------------------------------------
#>     1 |     never |   3 |  0.33 |    0.33 |   0.33
#>     2 | sometimes |  97 | 10.68 |   10.77 |  11.10
#>     3 |     often | 591 | 65.09 |   65.59 |  76.69
#>     4 |    always | 210 | 23.13 |   23.31 | 100.00
#>  <NA> |      <NA> |   7 |  0.77 |    <NA> |   <NA>
#> 
#> do you find caregiving too demanding? (c83cop2) <categorical> 
#> # total N=908 valid N=902 mean=2.02 sd=0.72
#> 
#> Value |     Label |   N | Raw % | Valid % | Cum. %
#> --------------------------------------------------
#>     1 |     Never | 186 | 20.48 |   20.62 |  20.62
#>     2 | Sometimes | 547 | 60.24 |   60.64 |  81.26
#>     3 |     Often | 130 | 14.32 |   14.41 |  95.68
#>     4 |    Always |  39 |  4.30 |    4.32 | 100.00
#>  <NA> |      <NA> |   6 |  0.66 |    <NA> |   <NA>
#> 
#> does caregiving cause difficulties in your relationship with your friends? (c84cop3) <categorical> 
#> # total N=908 valid N=902 mean=1.63 sd=0.87
#> 
#> Value |     Label |   N | Raw % | Valid % | Cum. %
#> --------------------------------------------------
#>     1 |     Never | 516 | 56.83 |   57.21 |  57.21
#>     2 | Sometimes | 252 | 27.75 |   27.94 |  85.14
#>     3 |     Often |  82 |  9.03 |    9.09 |  94.24
#>     4 |    Always |  52 |  5.73 |    5.76 | 100.00
#>  <NA> |      <NA> |   6 |  0.66 |    <NA> |   <NA>
ref_lvl(dat, c82cop1, c83cop2, lvl = 2) %>% frq()
#> do you feel you cope well as caregiver? (c82cop1) <categorical> 
#> # total N=908 valid N=901 mean=3.01 sd=0.82
#> 
#> Value |     Label |   N | Raw % | Valid % | Cum. %
#> --------------------------------------------------
#>     1 | sometimes |  97 | 10.68 |   10.77 |  10.77
#>     2 |     never |   3 |  0.33 |    0.33 |  11.10
#>     3 |     often | 591 | 65.09 |   65.59 |  76.69
#>     4 |    always | 210 | 23.13 |   23.31 | 100.00
#>  <NA> |      <NA> |   7 |  0.77 |    <NA> |   <NA>
#> 
#> do you find caregiving too demanding? (c83cop2) <categorical> 
#> # total N=908 valid N=902 mean=1.62 sd=0.88
#> 
#> Value |     Label |   N | Raw % | Valid % | Cum. %
#> --------------------------------------------------
#>     1 | Sometimes | 547 | 60.24 |   60.64 |  60.64
#>     2 |     Never | 186 | 20.48 |   20.62 |  81.26
#>     3 |     Often | 130 | 14.32 |   14.41 |  95.68
#>     4 |    Always |  39 |  4.30 |    4.32 | 100.00
#>  <NA> |      <NA> |   6 |  0.66 |    <NA> |   <NA>
#> 
#> does caregiving cause difficulties in your relationship with your friends? (c84cop3) <categorical> 
#> # total N=908 valid N=902 mean=1.63 sd=0.87
#> 
#> Value |     Label |   N | Raw % | Valid % | Cum. %
#> --------------------------------------------------
#>     1 |     Never | 516 | 56.83 |   57.21 |  57.21
#>     2 | Sometimes | 252 | 27.75 |   27.94 |  85.14
#>     3 |     Often |  82 |  9.03 |    9.09 |  94.24
#>     4 |    Always |  52 |  5.73 |    5.76 | 100.00
#>  <NA> |      <NA> |   6 |  0.66 |    <NA> |   <NA>

# compare numeric and string value for "lvl"-argument
x <- to_factor(efc$e42dep)
frq(x)
#> elder's dependency (x) <categorical> 
#> # total N=908 valid N=901 mean=2.94 sd=0.94
#> 
#> Value |                Label |   N | Raw % | Valid % | Cum. %
#> -------------------------------------------------------------
#>     1 |          independent |  66 |  7.27 |    7.33 |   7.33
#>     2 |   slightly dependent | 225 | 24.78 |   24.97 |  32.30
#>     3 | moderately dependent | 306 | 33.70 |   33.96 |  66.26
#>     4 |   severely dependent | 304 | 33.48 |   33.74 | 100.00
#>  <NA> |                 <NA> |   7 |  0.77 |    <NA> |   <NA>
ref_lvl(x, lvl = 2) %>% frq()
#> elder's dependency (x) <categorical> 
#> # total N=908 valid N=901 mean=2.76 sd=1.16
#> 
#> Value |                Label |   N | Raw % | Valid % | Cum. %
#> -------------------------------------------------------------
#>     1 |   slightly dependent | 225 | 24.78 |   24.97 |  24.97
#>     2 |          independent |  66 |  7.27 |    7.33 |  32.30
#>     3 | moderately dependent | 306 | 33.70 |   33.96 |  66.26
#>     4 |   severely dependent | 304 | 33.48 |   33.74 | 100.00
#>  <NA> |                 <NA> |   7 |  0.77 |    <NA> |   <NA>
ref_lvl(x, lvl = "slightly dependent") %>% frq()
#> elder's dependency (x) <categorical> 
#> # total N=908 valid N=901 mean=2.76 sd=1.16
#> 
#> Value |                Label |   N | Raw % | Valid % | Cum. %
#> -------------------------------------------------------------
#>     1 |   slightly dependent | 225 | 24.78 |   24.97 |  24.97
#>     2 |          independent |  66 |  7.27 |    7.33 |  32.30
#>     3 | moderately dependent | 306 | 33.70 |   33.96 |  66.26
#>     4 |   severely dependent | 304 | 33.48 |   33.74 | 100.00
#>  <NA> |                 <NA> |   7 |  0.77 |    <NA> |   <NA>

# factors with non-numeric factor levels
data(iris)
levels(iris$Species)
#> [1] "setosa"     "versicolor" "virginica" 
levels(ref_lvl(iris$Species, lvl = 3))
#> [1] "virginica"  "setosa"     "versicolor"
levels(ref_lvl(iris$Species, lvl = "versicolor"))
#> [1] "versicolor" "setosa"     "virginica"