Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Quantiles by Group calculation in R
    Quantiles by Group calculation in R with examples R
  • How to Label Outliers in Boxplots in ggplot2
    How to Label Outliers in Boxplots in ggplot2? R
  • A Side-by-Side Boxplot in R
    A Side-by-Side Boxplot in R: How to Do It R
  • How to perform MANOVA test in R
    How to perform the MANOVA test in R? R
  • How to create contingency tables in R
    How to create contingency tables in R? R
  • Top Data Modeling Tools for 2023
    Top Data Modeling Tools for 2023 Machine Learning
  • How to Calculate Lag by Group in R
    How to Calculate Lag by Group in R? R
  • How to Create a Frequency Table by Group in R
    How to Create a Frequency Table by Group in R? R
Tips for Rearranging Columns in R

Tips for Rearranging Columns in R

Posted on June 25June 24 By Jim No Comments on Tips for Rearranging Columns in R
Tweet
Share
Share
Pin

Tips for Rearranging Columns in R, you might frequently want to reorder the columns in a data frame.

The select() function from the dplyr package, fortunately, makes this simple to accomplish.

library(dplyr)

This tutorial shows several examples of how to use this function in practice using the following data frame.

Remove Rows from the data frame in R – Data Science Tutorials

Let’s create a data frame

df <- data.frame(player = c('P1', 'P2', 'P3', 'P4'),
                 points = c(124, 229, 313, 415),
                 result = c('Win', 'Loss', 'Win', 'Loss'))

Now we can view the data frame

df
   player points result
1     P1    124    Win
2     P2    229   Loss
3     P3    313    Win
4     P4    415   Loss

Example 1: A Column is Moved to the First Position

How to shift a particular column in a data frame to the front position is demonstrated by the code below.

Let’s move column ‘result’ to the first position

df %>% select(result, everything())
   result player points
1    Win     P1    124
2   Loss     P2    229
3    Win     P3    313
4   Loss     P4    415

This code instructs dplyr to initially select the points column before including every other column.

Descriptive statistics vs Inferential statistics: Guide – Data Science Tutorials

Example 2: Move a Column to the Last Position

How to relocate a particular column in a data frame to the last location is demonstrated by the code below:

relocate ‘player’ column to last place

df %>% select(-player, player)
   points result player
1    124    Win     P1
2    229   Loss     P2
3    313    Win     P3
4    415   Loss     P4

Dplyr is instructed by this code to first choose all columns other than the player column, then to select the player column once more.

How to perform the Kruskal-Wallis test in R? – Data Science Tutorials

The player column is thus moved to the bottom of the data frame as a result of this.

Example 3: Rearrange Several Columns

The code that follows demonstrates how to sequentially reorder multiple columns.

df %>% select(points, result, player)
   points result player
1    124    Win     P1
2    229   Loss     P2
3    313    Win     P3
4    415   Loss     P4

Example 4: Alphabetically reorder the columns

The code below demonstrates how to alphabetically arrange the columns.

How to Perform a Log Rank Test in R – Data Science Tutorials

alphabetize the columns

df %>% select(order(colnames(.)))
   player points result
1     P1    124    Win
2     P2    229   Loss
3     P3    313    Win
4     P4    415   Loss

Example 5: Reverse Column Order

Reversing the column order in a data frame is demonstrated by the code below.

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

column order in reverse

df %>% select(result:player, everything())
   result points player
1    Win    124     P1
2   Loss    229     P2
3    Win    313     P3
4   Loss    415     P4

Check your inbox or spam folder to confirm your subscription.

Tweet
Share
Share
Pin
R Tags:dplyr

Post navigation

Previous Post: How to Recode Values in R
Next Post: How to Group and Summarize Data in R

Related Posts

  • How to Join Multiple Data Frames in R
    How to Join Multiple Data Frames in R R
  • How to Recode Values in R
    How to Recode Values in R R
  • Dealing Missing values in R
    Dealing With Missing values in R R
  • Box Cox transformation in R
    Box Cox transformation in R R
  • Difference between R and Python
    Difference between R and Python R
  • Two-Way ANOVA Example in R
    How to perform One-Sample Wilcoxon Signed Rank Test 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
  • Defensive Programming Strategies in R
  • Plot categorical data in R
  • Top Data Modeling Tools for 2023
  • Ogive Graph in R
  • Is R or Python Better for Data Science in Bangalore

Check your inbox or spam folder to confirm your subscription.

  • Data Scientist Career Path Map in Finance
  • Is Python the ideal language for machine learning
  • Convert character string to name class object
  • How to play sound at end of R Script
  • Pattern Searching in R
  • How Do Online Criminals Acquire Sensitive Data
    How Do Online Criminals Acquire Sensitive Data Machine Learning
  • Artificial Intelligence Examples
    Artificial Intelligence Examples-Quick View Course
  • How to Create a Frequency Table by Group in R
    How to Create a Frequency Table by Group in R? R
  • Beginner's Guide to Data Science
    Beginner’s Guide to Data Science Machine Learning
  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • How to Use Spread Function in R
    How to Use Spread Function in R?-tidyr Part1 R
  • Comparison between Statistics and Luck
    Lottery Prediction-Comparison between Statistics and Luck Machine Learning
  • How to perform MANOVA test in R
    How to perform the MANOVA test in R? R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme