Convert Multiple Columns to Numeric in R

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Convert Multiple Columns to Numeric in R appeared first on Data Science Tutorials

Convert Multiple Columns to Numeric in R, Using the dplyr package, you can change many columns to numeric using the following techniques.

The examples that follow demonstrate each technique in action.

Calculate the p-Value from Z-Score in R – Data Science Tutorials

Example 1: Convert Specific Columns to Numeric

Let’s say we have the R data frame shown below:

df <- data.frame(team=c('TeamA', 'TeamB', 'TeamC', 'TeamD', 'TeamE'),
                 position=c('POS-1', 'POS-1', 'POS-1', 'POS-2', 'POS-2'),
                 assists=c('323', '528', '351', '239', '634'),
                 rebounds=c('230', '228', '124', '324', '128'))

Now we can view the structure of the data frame

str(df)
'data.frame':      5 obs. of  4 variables:
 $ team    : chr  "TeamA" "TeamB" "TeamC" "TeamD" ...
 $ position: chr  "POS-1" "POS-1" "POS-1" "POS-2" ...
 $ assists : chr  "323" "528" "351" "239" ...
 $ rebounds: chr  "230" "228" "124" "324" ...

Every column in the data frame is currently a character, as can be seen.

We may use the following code to only numeric the columns for assists and rebounds.

How to perform a one-sample t-test in R? – Data Science Tutorials

library(dplyr)
df <- df %>% mutate_at(c('assists', 'rebounds'), as.numeric)

display the changed data frame’s structure

str(df)
'data.frame':      5 obs. of  4 variables:
 $ team    : chr  "TeamA" "TeamB" "TeamC" "TeamD" ...
 $ position: chr  "POS-1" "POS-1" "POS-1" "POS-2" ...
 $ assists : num  323 528 351 239 634
 $ rebounds: num  230 228 124 324 128

The columns for rebounds and assists are now both numeric, as we can see.

Example 2: Transform every character column to a number

Let’s say we have the R data frame shown below

Let’s create a data frame

df <- data.frame(ranking=factor(c(11, 14, 13, 11, 12)),
                 assists=c('102', '120', '68', '151', '415'),
                 points=c('313', '128', '231', '339', '534'),
                 rebounds=c('450', '280', '241', '242', '282'))

Let’s view the structure of the data frame

Two Sample Proportions test in R-Complete Guide – Data Science Tutorials

str(df)
'data.frame':      5 obs. of  4 variables:
 $ ranking : Factor w/ 4 levels "11","12","13",..: 1 4 3 1 2
 $ assists : chr  "102" "120" "68" "151" ...
 $ points  : chr  "313" "128" "231" "339" ...
 $ rebounds: chr  "450" "280" "241" "242" ...

Three of the data frame’s columns are character columns, as can be seen.

We can employ the following syntax to change all character columns to numbers:

library(dplyr)
df <- df %>% mutate_if(is.character, as.numeric)

Now we can view the structure of the updated data frame

Dealing With Missing values in R – Data Science Tutorials

str(df)
'data.frame':      5 obs. of  4 variables:
 $ ranking : Factor w/ 4 levels "11","12","13",..: 1 4 3 1 2
 $ assists : num  102 120 68 151 415
 $ points  : num  313 128 231 339 534
 $ rebounds: num  450 280 241 242 282

The character columns are now fully numerical, as can be seen.

The post Convert Multiple Columns to Numeric in R appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)