How to Use as.numeric() Function in R


Often you may want to convert character vectors to numeric vectors in R.

One of the easiest ways to do so is by using the as.numeric() function from base R, which can be used to perform this exact task.

The as.numeric() function uses the following syntax:

as.numeric(x, …)

where:

  • x: Name of the object to be converted to numeric

The following examples show how to use the as.numeric() function in practice with a data frame in R.

Example: How to Use the as.numeric() Function in R

Suppose we create the following data frame in R that contains information about various basketball players, including their team name, points scored, and total assists:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
                 points=c('12', '13', '20', '40', '34', '14', '28', '19'),
                 assists=c('7', '4', '5', '9', '12', '0', '4', '12'))

#view data frame
df

We can view the data type of each column in the data frame by using the str() function, which is designed to return the “structure” of a data frame including the data type of each column.

We can use the following syntax to do so:

#view data type of each column in data frame
str(df)

'data.frame':	8 obs. of  3 variables:
 $ team   : chr  "A" "A" "A" "A" ...
 $ points : chr  "12" "13" "20" "40" ...
 $ assists: chr  "7" "4" "5" "9" ...

The output displays the data type of each column in the data frame.

From the output we can see:

  • team: This is a character vector
  • points: This is a character vector
  • assists: This is a character vector

The team column should be a character vector since it contains strings, but the points and assists columns should both be numeric vectors since they contain numerical values.

Suppose that we would like to convert just the points column to a numeric data type.

We can use the following syntax with the as.numeric() function to do so:

#convert only points column to numeric
df$points <- as.numeric(df$points)

#view updated data type of each column in data frame
str(df)

'data.frame':	8 obs. of  3 variables:
 $ team   : chr  "A" "A" "A" "A" ...
 $ points : num  12 13 20 40 34 14 28 19
 $ assists: chr  "7" "4" "5" "9" ...

We can see that the points column now has num as the data type, which indicates that it has successfully been converted to a numeric vector.

Suppose instead that we would like to convert both the points and assists columns to numeric vectors at the same time.

We can use the sapply() function along with the as.numeric() function to do so:

#convert points and assists columns to numeric
df[c('points', 'assists')] <- sapply(df[c('points', 'assists')], as.numeric)

#view updated data type of each column in data frame
str(df)
'data.frame':	8 obs. of  3 variables:
 $ team   : chr  "A" "A" "A" "A" ...
 $ points : num  12 13 20 40 34 14 28 19
 $ assists: num  7 4 5 9 12 0 4 12

We can see that the points and assists columns both have a data type of num now, which means both columns have been successfully converted to numeric vectors.

Note: The sapply function in R is used to apply a function to multiple columns in a data frame. In this scenario, we used it to apply the as.numeric() function to both the points and assists columns of the data frame.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use str_replace in R
How to Use str_split in R
How to Use str_detect in R
How to Use str_count in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *