Skip to content

Data Science Tutorials

For Data Science Learners

  • Calculating Conditional Probability in R
    Calculating Conditional Probability in R R
  • How to Use Mutate function in R
    How to Use Mutate function in R R
  • How to add columns to a data frame in R
    How to add columns to a data frame in R R
  • How to Find Quartiles in R
    How to Find Quartiles in R? R
  • One proportion Z Test in R
    One proportion Z Test in R R
  • The Uniform Distribution in R
    The Uniform Distribution in R R
  • How to get the last value of each group in R
    How to get the last value of each group in R R
  • Add Footnote to ggplot2 R
Convert Multiple Columns to Numeric in R

Convert Multiple Columns to Numeric in R

Posted on July 10July 8 By Admin No Comments on 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.

Check your inbox or spam folder to confirm your subscription.

R Tags:dplyr

Post navigation

Previous Post: How to Rank by Group in R?
Next Post: How to Use Mutate function in R

Related Posts

  • optim Function in R R
  • How to Standardize Data in R
    How to Standardize Data in R? R
  • How to Calculate Ratios in R
    How to Calculate Ratios in R R
  • How to Use Mutate function in R
    How to Use Mutate function in R R
  • Convert multiple columns into a single column
    Convert multiple columns into a single column-tidyr Part4 R
  • Crosstab calculation in R
    Crosstab calculation in R R

Leave a Reply Cancel reply

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

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Sort or Order Rank in R R
  • Calculating Autocorrelation in R R
  • Type II Error in R
    Type II Error in R R
  • The Uniform Distribution in R
    The Uniform Distribution in R R
  • Making games in R- Nara and eventloop Game Changers
    Making games in R- Nara and eventloop Game Changers Machine Learning
  • How to create summary table in R
    How to create summary table in R R
  • How to plot categorical data in R
    Plot categorical data in R R
  • Data Science Strategies for Improving Customer Experience in R
    Data Science Strategies for Improving Customer Experience in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme