Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Remove Rows from the data frame in R
    Remove Rows from the data frame in R R
  • Get the first value in each group in R
    Get the first value in each group in R? R
  • Replace NA with Zero in R
    Replace NA with Zero in R R
  • droplevels in R with examples
    droplevels in R with examples R
  • Calculate the P-Value from Chi-Square Statistic in R
    Calculate the P-Value from Chi-Square Statistic in R R
  • How to Replace String in Column in R
    How to Replace String in Column using R R
  • How to Create Summary Tables in R
    How to Create Summary Tables in R R
  • Artificial Intelligence Examples
    Artificial Intelligence Examples-Quick View Course
How to change the column positions in R?

How to change the column positions in R?

Posted on July 15July 15 By Jim No Comments on How to change the column positions in R?
Tweet
Share
Share
Pin

How to change the column positions in R, you can rearrange the columns in a data frame by using the relocate() function from the dplyr package.

The following techniques can be used to alter the column positions.

Method 1: Move One Column to Front

move ‘x’ column to the front

df %>% relocate(x)

Method 2: Move Several Columns to Front

move ‘x’ and ‘y’ columns to the front

Best Books on Data Science with Python – Data Science Tutorials

df %>% relocate(x, y)

Method 3: Place Column After Another Column in New Position

move ‘x’ column to the position after ‘y’ column

df %>% relocate(x, .after=y)

Method 4: Place Column Before Another Column by Moving the Column

move ‘x’ column to position before ‘y’ column

df %>% relocate(x, .before=y)

The examples that follow demonstrate how to use each technique with the given data frame.

Artificial Intelligence Examples-Quick View – Data Science Tutorials

Let’s make a dataset

df <- data.frame(team=c('P1', 'P1', 'P1', 'P1', 'P2', 'P2', 'P2'),
points=c(110, 112, 123, 154, 215, 146, 87),
assists=c(81, 75, 22, 33, 52, 29, 70),
rebounds=c(46, 56, 18, 19, 87, 80, 93))

Now we can view the dataset

df
   team points assists rebounds
1   P1    110      81       46
2   P1    112      75       56
3   P1    123      22       18
4   P1    154      33       19
5   P2    215      52       87
6   P2    146      29       80
7   P2     87      70       93

Example 1: Move One Column to Front

The relocate() function can be used to move one column to the front as demonstrated by the code below.

Best Books to Learn R Programming – Data Science Tutorials

column “assists” to the front

df %>% relocate(assists)
   assists team points rebounds
1      81   P1    110       46
2      75   P1    112       56
3      22   P1    123       18
4      33   P1    154       19
5      52   P2    215       87
6      29   P2    146       80
7      70   P2     87       93

Example 2: Move a few columns forward

The relocate() function can be used to advance multiple columns by using the following code.

Best Data Science YouTube Tutorials Free to Learn – Data Science Tutorials

shift “points” and “assistances” to the front

df %>% relocate(points, assists)
  points assists team rebounds
1    110      81   P1       46
2    112      75   P1       56
3    123      22   P1       18
4    154      33   P1       19
5    215      52   P2       87
6    146      29   P2       80
7     87      70   P2       93

Example 3: Place Column After Another Column in New Position

The relocate() function can be used to position one column behind another column by using the following code.

5 Free Books to Learn Statistics For Data Science – Data Science Tutorials

place the “team” column after the “assistances” column

df %>% relocate(team, .after=assists)
    points assists team rebounds
1    110      81   P1       46
2    112      75   P1       56
3    123      22   P1       18
4    154      33   P1       19
5    215      52   P2       87
6    146      29   P2       80
7     87      70   P2       93

Example 4: Place Column Before Another Column by Moving the Column

The relocate() function can be used to move one column ahead of another column by providing the following code.

Free Best Online Course For Statistics – Data Science Tutorials

place the “team” column before the “rebounds” column.

df %>% relocate(team, .before=rebounds)
   points assists team rebounds
1    110      81   P1       46
2    112      75   P1       56
3    123      22   P1       18
4    154      33   P1       19
5    215      52   P2       87
6    146      29   P2       80
7     87      70   P2       93

Check your inbox or spam folder to confirm your subscription.

Tweet
Share
Share
Pin
R Tags:dplyr

Post navigation

Previous Post: Subset rows based on their integer locations-slice in R
Next Post: A Side-by-Side Boxplot in R: How to Do It

Related Posts

  • Augmented Dickey-Fuller Test in R
    Augmented Dickey-Fuller Test in R R
  • How to Create a Frequency Table by Group in R
    How to Create a Frequency Table by Group in R? R
  • A Side-by-Side Boxplot in R
    A Side-by-Side Boxplot in R: How to Do It R
  • Crosstab calculation in R
    Crosstab calculation in R R
  • How to Turn Off Scientific Notation in R
    How to Turn Off Scientific Notation in R? R
  • gganatogram Plot in R
    How to create Anatogram plot 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
  • 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
  • Load Multiple Packages in R
    Load Multiple Packages in R R
  • Filtering for Unique Values
    Filtering for Unique Values in R- Using the dplyr R
  • How to Rank by Group in R?
    How to Rank by Group in R? R
  • How do augmented analytics work
    How do augmented analytics work? R
  • How to compare variances in R
    How to compare variances in R R
  • Subsetting with multiple conditions in R
    Subsetting with multiple conditions in R R
  • Comparing group means in R
    One way ANOVA Example in R-Quick Guide R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme