as.numeric() Function in R

as.numeric() is a built-in function in R that allows you to convert a character vector into a numeric vector. Therefore, it returns a numeric vector as an output.

Syntax

as.numeric(x)

Argument

  • x = character vector

Example

# creating a character vector
char_data <- c('25', '50', '75', '100', '125')
# converting the character vector to a numeric vector
num_data <- as.numeric(char_data)
char_data
# confirming the vector has been converted
is.numeric(num_data)

Output

[1] "25" "50" "75" "100" "125"
[1] TRUE

In the above example, we use is.numeric() to confirm if the vector has been converted to a numeric vector or not. The function returned TRUE, meaning the vector is numeric.

Leave a Comment