How to change the column positions in R?

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

You may also like...

Leave a Reply

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

15 + four =