This function extracts the first (and optionally second) or last digits in a vector.

extract_digits(x, check = 'first', include.zero = FALSE)

Arguments

x

a numeric vector.

check

location of the digits to extract. Can be first, firsttwo, or last.

include.zero

logical. Whether to include the digit zero in the output.

Value

A vector of first (and optionally second) or last digits.

Author

Koen Derks, k.derks@nyenrode.nl

Examples

set.seed(1)
x <- rnorm(100)

# Extract first digits (without zero)
extract_digits(x, check = 'first')
#>   [1] 6 1 8 1 3 8 4 7 5 3 1 3 6 2 1 4 1 9 8 5 9 7 7 1 6 5 1 1 4 4 1 1 3 5 1 4 3
#>  [38] 5 1 7 1 2 6 5 6 7 3 7 1 8 3 6 3 1 1 1 3 1 5 1 2 3 6 2 7 1 1 1 1 2 4 7 6 9
#>  [75] 1 2 4 1 7 5 5 1 1 1 5 3 1 3 3 2 5 1 1 7 1 5 1 5 1 4

# Extract last digits (including zero)
extract_digits(x, check = 'last', include.zero = TRUE)
#>   [1] 3 8 4 0 3 2 9 4 8 1 1 9 2 1 2 4 2 4 2 9 2 8 7 9 2 6 6 7 8 2 6 0 9 5 8 1 9
#>  [38] 6 0 6 6 5 0 6 9 1 6 7 1 8 0 1 4 3 3 8 7 4 7 4 0 4 9 3 4 9 0 7 5 7 8 1 1 3
#>  [75] 5 9 4 0 7 9 7 4 8 2 9 3 6 0 7 7 4 1 6 0 9 6 8 7 2 7