Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Contact
  • About Us
  • Toggle search form
  • How to Use Mutate function in R
    How to Use Mutate function in R R
  • How to Rank by Group in R?
    How to Rank by Group in R? R
  • Subset rows based on their integer locations
    Subset rows based on their integer locations-slice in R R
  • one-sample-proportion-test-in-r
    One sample proportion test in R-Complete Guide R
  • How to Create Summary Tables in R
    How to Create Summary Tables in R R
  • How to add columns to a data frame in R
    How to add columns to a data frame in R R
  • Best Data Science YouTube Tutorials
    Best Data Science YouTube Tutorials Free to Learn Course
  • How to do Conditional Mutate in R
    How to do Conditional Mutate 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

  • Remove Rows from the data frame in R
    Remove Rows from the data frame in R R
  • Rounded corner bar plot in R
    How to make a rounded corner bar plot in R? R
  • How to Add Superscripts and Subscripts to Plots in R?, The basic syntax for adding superscripts or subscripts to charts in R is as follows:
    How to Add Superscripts and Subscripts to Plots in R? R
  • Error: Can't rename columns that don't exist
    Can’t rename columns that don’t exist R
  • How to change the column positions in R?
    How to change the column positions in R? R
  • How to apply a transformation to multiple columns in R?
    How to apply a transformation to multiple columns in R? R

Leave a Reply Cancel reply

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




  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Is Data Science a Dying Profession?
  • How to Label Outliers in Boxplots in ggplot2?
  • Best Books About Data Analytics
  • How to Scale Only Numeric Columns in R
  • Best Books to Learn Statistics for Data Science

Check your inbox or spam folder to confirm your subscription.




 https://www.r-bloggers.com
  • Convert Multiple Columns to Numeric in R
    Convert Multiple Columns to Numeric in R R
  • Rounded corner bar plot in R
    How to make a rounded corner bar plot in R? R
  • Control Chart in Quality Control
    Control Chart in Quality Control-Quick Guide Statistics
  • Best online course for R programming
    Best online course for R programming Course
  • How to Replace String in Column in R
    How to Replace String in Column using R R
  • Two-Way ANOVA Example in R
    How to perform One-Sample Wilcoxon Signed Rank Test in R? R
  • How to add columns to a data frame in R
    How to add columns to a data frame in R R
  • Arrange the rows in a specific sequence in R
    Arrange the rows in a specific sequence in R R

Copyright © 2022 Data Science Tutorials.

Powered by PressBook News WordPress theme