Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Error in sum(List) : invalid 'type' (list) of argument
    Error in sum(List) : invalid ‘type’ (list) of argument R
  • Control Chart in Quality Control
    Control Chart in Quality Control-Quick Guide Statistics
  • How to Implement the Sklearn Predict Approach
    How to Implement the Sklearn Predict Approach? R
  • How to Perform Bootstrapping in R
    How to Perform Bootstrapping in R R
  • Methods for Integrating R and Hadoop
    Methods for Integrating R and Hadoop complete Guide R
  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • How to Standardize Data in R
    How to Standardize Data in R? R
  • Subset rows based on their integer locations
    Subset rows based on their integer locations-slice 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

  • Tips for Rearranging Columns in R
    Tips for Rearranging Columns in R R
  • How to create a heatmap in R
    How to create a heatmap in R R
  • How to Group and Summarize Data in R
    How to Group and Summarize Data in R R
  • Error in rbind(deparse.level ...) numbers of columns of arguments do not match
    Error in rbind(deparse.level …) numbers of columns of arguments do not match R
  • how to draw heatmap in r
    How to draw heatmap in r: Quick and Easy way R
  • How to Replace Inf Values with NA in R
    How to Replace Inf Values with NA in R R

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
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • test for normal distribution in r
    Test for Normal Distribution in R-Quick Guide R
  • How to Replace Inf Values with NA in R
    How to Replace Inf Values with NA in R R
  • Count Observations by Group in R
    Count Observations by Group in R R
  • droplevels in R with examples
    droplevels in R with examples R
  • How to put margins on tables or arrays in R?
    How to put margins on tables or arrays in R? R
  • How to Label Outliers in Boxplots in ggplot2
    How to Label Outliers in Boxplots in ggplot2? R
  • Load Multiple Packages in R
    Load Multiple Packages in R R
  • How to create a heatmap in R
    How to create a heatmap in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme