Convert Multiple Columns to Numeric in R

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.

You may also like...

Leave a Reply

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

one × four =