How to change the column positions in R?

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to change the column positions in R? appeared first on Data Science Tutorials

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

The post How to change the column positions in R? appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)