4.2 C
London
Friday, December 20, 2024
HomeTidyverse in Rdplyr in RHow to Use the relocate() Function in dplyr (With Examples)

How to Use the relocate() Function in dplyr (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the relocate() function from the dplyr package in R to change the column positions in a data frame.

You can use the following methods to change the column positions:

Method 1: Move One Column to Front

#move 'x' column to front
df %>% relocate(x)

Method 2: Move Several Columns to Front

#move 'x' and 'y' columns to front
df %>% relocate(x, y)

Method 3: Move Column to Position After Another Column

#move 'x' column to position after 'y' column
df %>% relocate(x, .after=y)

Method 4: Move Column to Position Before Another Column

#move 'x' column to position before 'y' column
df %>% relocate(x, .before=y)

The following examples show how to each method with the following data frame:

#create dataset
df frame(team=c('A', 'A', 'A', 'B', 'B', 'C', 'C'),
                 points=c(1, 2, 3, 4, 5, 6, 7),
                 assists=c(1, 5, 2, 3, 2, 2, 0),
                 rebounds=c(6, 6, 10, 12, 8, 8, 3))

#view dataset
df

  team points assists rebounds
1    A      1       1        6
2    A      2       5        6
3    A      3       2       10
4    B      4       3       12
5    B      5       2        8
6    C      6       2        8
7    C      7       0        3

Example 1: Move One Column to Front

The following code shows how to use the relocate() function to move one column to the front:

#move 'assists' column to front
df %>% relocate(assists)

  assists team points rebounds
1       1    A      1        6
2       5    A      2        6
3       2    A      3       10
4       3    B      4       12
5       2    B      5        8
6       2    C      6        8
7       0    C      7        3

Example 2: Move Several Columns to Front

The following code shows how to use the relocate() function to move multiple columns to the front:

#move 'points' and 'assists' to front
df %>% relocate(points, assists)

  points assists team rebounds
1      1       1    A        6
2      2       5    A        6
3      3       2    A       10
4      4       3    B       12
5      5       2    B        8
6      6       2    C        8
7      7       0    C        3

Example 3: Move Column to Position After Another Column

The following code shows how to use the relocate() function to move one column to a specific position after another column:

#move 'team' column to after 'assists' column
df %>% relocate(team, .after=assists)

  points assists team rebounds
1      1       1    A        6
2      2       5    A        6
3      3       2    A       10
4      4       3    B       12
5      5       2    B        8
6      6       2    C        8
7      7       0    C        3

Example 4: Move Column to Position Before Another Column

The following code shows how to use the relocate() function to move one column to a specific position before another column:

#move 'team' column to before 'rebounds' column
df %>% relocate(team, .before=rebounds)

  points assists team rebounds
1      1       1    A        6
2      2       5    A        6
3      3       2    A       10
4      4       3    B       12
5      5       2    B        8
6      6       2    C        8
7      7       0    C        3

Additional Resources

The following tutorials explain how to perform other common functions using dplyr:

How to Remove Rows Using dplyr
How to Arrange Rows Using dplyr
How to Filter by Multiple Conditions Using dplyr

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories