# How to Convert Column to Numeric in R

You can easily convert a character vector into a numeric, but only if its vector items are “convertible” to numeric. If there’s one character item in a vector, you will get an error while converting that vector to numerical. Let me demonstrate this by an example.

``````rv <- c(21, "k", 19, 11, "b", "l")
rv
mode(rv)
class(rv)

rn <- as.numeric(rv)
rn``````

#### Output

`````` "21"  "k"  "19"  "11"  "b"  "l"
 "character"
 "character"

Warning message:
NAs introduced by coercion

 21  NA  19  11  NA  NA``````

To check the class of the variable, use the class() function in R.

As you can see that if the vector contains a character then, it won’t be able to convert that element into a numeric. It throws a warning message:  NAs introduced by coercion.

It means the values which can not be converted to numerics become NA, as you can see in the output.

Now, let’s see how to convert a data frame column to numerical values in R.

## Convert column to numeric R

To convert a column to numeric in R, use the as.numeric() function. The as.numeric() is a built-in R function that returns a numeric value or converts any value to a numeric value.

If you are working with a data frame then you often have requirements where you need to convert a specific column to numeric, and let’s see how to do it.

To create a data frame in R, use the data.frame() function.

``````df <- data.frame(col1 = c("19", "21", "11"), col2 = c(19, 21, "11"))
print(df)
sapply(df, class)

df\$col1 <- as.numeric(as.character(df\$col1))
print(df)
sapply(df, class)``````

#### Output

``````  col1         col2
1 19            19
2 21            21
3 11            11

col1           col2
"character"  "character"

col1         col2
1  19           19
2  21           21
3  11           11

col1          col2
"numeric"     "character"``````

You can see that the first column is converted from character to numeric.

## Convert all columns of a data frame to numeric in R

To convert all the columns of the data frame to numeric in R, use the lapply() function to loop over the columns and convert to numeric by first converting it to character class as the columns were a factor.

``````df <- data.frame(col1 = c("19", "21", "11"), col2 = c(19, 21, "11"))
print(df)
sapply(df, class)

df[] <- lapply(df, function(x) as.numeric(as.character(x)))
df
sapply(df, class)``````

#### Output

``````   col1        col2
1   19          19
2   21          21
3   11          11

col1         col2
"character"   "character"

col1        col2
1   19          19
2   21          21
3   11          11

col1         col2
"numeric"    "numeric"``````

You can see that the lapply() function helps us to convert all the columns into numeric in the output. If the values are compatible, it will convert it; otherwise, it will give you an error.

If you have ranges of variables to convert,  you can also use the sapply() function in R. The apply() is a built-in R function that converts the dataset to a matrix and the matrix can hold only a single class.

That’s it for this tutorial.

Categories R