This function converts (replaces) factor levels with the related factor level index number, thus the factor is converted to a numeric variable.

as_numeric(x, ...)

to_numeric(x, ...)

# S3 method for data.frame
as_numeric(x, ..., start.at = NULL, keep.labels = TRUE, use.labels = FALSE)

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'.

start.at

Starting index, i.e. the lowest numeric value of the variable's value range. By default, this argument is NULL, hence the lowest value of the returned numeric variable corresponds to the lowest factor level (if factor levels are numeric) or to 1 (if factor levels are not numeric).

keep.labels

Logical, if TRUE, former factor levels will be added as value labels. For numeric factor levels, values labels will be used, if present. See 'Examples' and set_labels for more details.

use.labels

Logical, if TRUE and x has numeric value labels, the values defined in the labels (right-hand side of labels, for instance labels = c(null = 0, one = 1)) will be set as numeric values (instead of consecutive factor level numbers). See 'Examples'.

Value

A numeric variable with values ranging either from start.at to

start.at + length of factor levels, or to the corresponding factor levels (if these were numeric). If x is a data frame, the complete data frame x will be returned, where variables specified in ... are coerced to numeric; if ... is not specified, applies to all variables in the data frame.

Examples

data(efc)
test <- as_label(efc$e42dep)
table(test)
#> test
#> <dbl> 
#>     1 

table(as_numeric(test))
#> 
#> 1 
#> 1 
hist(as_numeric(test, start.at = 0))


# set lowest value of new variable to "5".
table(as_numeric(test, start.at = 5))
#> 
#> 5 
#> 1 

# numeric factor keeps values
dummy <- factor(c("3", "4", "6"))
table(as_numeric(dummy))
#> 
#> 3 4 6 
#> 1 1 1 

# do not drop unused factor levels
dummy <- ordered(c(rep("No", 5), rep("Maybe", 3)),
                 levels = c("Yes", "No", "Maybe"))
as_numeric(dummy)
#> [1] 2 2 2 2 2 3 3 3
#> attr(,"labels")
#>   Yes    No Maybe 
#>     1     2     3 

# non-numeric factor is converted to numeric
# starting at 1
dummy <- factor(c("D", "F", "H"))
table(as_numeric(dummy))
#> 
#> 1 2 3 
#> 1 1 1 

# for numeric factor levels, value labels will be used, if present
dummy1 <- factor(c("3", "4", "6"))
dummy1 <- set_labels(dummy1, labels = c("first", "2nd", "3rd"))
dummy1
#> [1] 3 4 6
#> attr(,"labels")
#> first   2nd   3rd 
#>     3     4     6 
#> Levels: 3 4 6
as_numeric(dummy1)
#> [1] 3 4 6
#> attr(,"labels")
#> first   2nd   3rd 
#>     3     4     6 

# for non-numeric factor levels, these will be used.
# value labels will be ignored
dummy2 <- factor(c("D", "F", "H"))
dummy2 <- set_labels(dummy2, labels = c("first", "2nd", "3rd"))
dummy2
#> [1] D F H
#> attr(,"labels")
#> first   2nd   3rd 
#>     D     F     H 
#> Levels: D F H
as_numeric(dummy2)
#> [1] 1 2 3
#> attr(,"labels")
#> D F H 
#> 1 2 3 


# easily coerce specific variables in a data frame to numeric
# and keep other variables, with their class preserved
data(efc)
efc$e42dep <- as.factor(efc$e42dep)
efc$e16sex <- as.factor(efc$e16sex)
efc$e17age <- as.factor(efc$e17age)

# convert back "sex" and "age" into numeric
head(as_numeric(efc, e16sex, e17age))
#>   c12hour e15relat e16sex e17age e42dep c82cop1 c83cop2 c84cop3 c85cop4 c86cop5
#> 1      16        2      2     83      3       3       2       2       2       1
#> 2     148        2      2     88      3       3       3       3       3       4
#> 3      70        1      2     82      3       2       2       1       4       1
#> 4     168        1      2     67      4       4       1       3       1       1
#> 5     168        2      2     84      4       3       2       1       2       2
#> 6      16        2      2     85      4       2       2       3       3       3
#>   c87cop6 c88cop7 c89cop8 c90cop9 c160age c161sex c172code c175empl barthtot
#> 1       1       2       3       3      56       2        2        1       75
#> 2       1       3       2       2      54       2        2        1       75
#> 3       1       1       4       3      80       1        1        0       35
#> 4       1       1       2       4      69       1        2        0        0
#> 5       2       1       4       4      47       2        2        0       25
#> 6       2       2       1       1      56       1        2        1       60
#>   neg_c_7 pos_v_4 quol_5 resttotn tot_sc_e n4pstu nur_pst
#> 1      12      12     14        0        4      0      NA
#> 2      20      11     10        4        0      0      NA
#> 3      11      13      7        0        1      2       2
#> 4      10      15     12        2        0      3       3
#> 5      12      15     19        2        1      2       2
#> 6      19       9      8        1        3      2       2

x <- factor(c("None", "Little", "Some", "Lots"))
x <- set_labels(x,
  labels = c(None = "0.5", Little = "1.3", Some = "1.8", Lots = ".2")
)
x
#> [1] None   Little Some   Lots  
#> attr(,"labels")
#>   Lots   None Little   Some 
#>    0.2    0.5    1.3    1.8 
#> Levels: Little Lots None Some
as_numeric(x)
#> [1] 3 1 4 2
#> attr(,"labels")
#> Little   Lots   None   Some 
#>      1      2      3      4 
as_numeric(x, use.labels = TRUE)
#> [1] 1.3 0.2 1.8 0.5
#> attr(,"labels")
#> Little   Lots   None   Some 
#>    0.2    0.5    1.3    1.8 
as_numeric(x, use.labels = TRUE, keep.labels = FALSE)
#> [1] 1.3 0.2 1.8 0.5