Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • gganatogram Plot in R
    How to create Anatogram plot in R R
  • Best Books on Data Science with Python
    Best Books on Data Science with Python Course
  • How to Filter Rows In R
    How to Filter Rows In R? R
  • Two-Way ANOVA Example in R
    How to perform One-Sample Wilcoxon Signed Rank Test in R? R
  • Best Books to Learn R Programming
    Best Books to Learn R Programming Course
  • similarity measure between two populations
    Similarity Measure Between Two Populations-Brunner Munzel Test Statistics
  • how to create a hexbins chart in R
    How to create a hexbin chart in R R
  • How to Standardize Data in R
    How to Standardize Data in R? R
Convert Multiple Columns to Numeric in R

Convert Multiple Columns to Numeric in R

Posted on July 10July 8 By Jim No Comments on Convert Multiple Columns to Numeric in R
Tweet
Share
Share
Pin

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.

Tweet
Share
Share
Pin
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

  • Comparing group means in R
    One way ANOVA Example in R-Quick Guide R
  • glm function in R
    glm function in r-Generalized Linear Models R
  • R Percentage by Group Calculation
    R Percentage by Group Calculation R
  • Two-Way ANOVA Example in R
    How to perform One-Sample Wilcoxon Signed Rank Test in R? R
  • droplevels in R with examples
    droplevels in R with examples R
  • best books about data analytics
    Best Books About Data Analytics Course

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Tips for Data Scientist Interview Openings
  • What is Epoch in Machine Learning?
  • Dynamic data visualizations in R
  • How Do Machine Learning Chatbots Work
  • Convex optimization role in machine learning

Check your inbox or spam folder to confirm your subscription.

  • Sampling from the population in R
  • Two of the Best Online Data Science Courses for 2023
  • Process of Machine Learning Optimisation?
  • ggplot2 scale in R (grammar for graphics)
  • ggplot aesthetics in R (Grammer of graphics)
  • similarity measure between two populations
    Similarity Measure Between Two Populations-Brunner Munzel Test Statistics
  • Detecting and Dealing with Outliers
    Detecting and Dealing with Outliers: First Step R
  • How to Use “not in” operator in Filter
    How to Use “not in” operator in Filter R
  • How to Avoid Overfitting
    How to Avoid Overfitting? Machine Learning
  • Load Multiple Packages in R
    Load Multiple Packages in R R
  • Hypothesis Testing in R
    Hypothesis Testing in R R
  • Subsetting with multiple conditions in R
    Subsetting with multiple conditions in R R
  • How to compare variances in R
    How to compare variances in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme