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