Hand holding pencil reviewing colorful data charts on desk with laptop.

The right way to Significance the relocate() Serve as in dplyr (With Examples)

Posted on
banner 336x280

You’ll be able to virtue the relocate() serve as from the dplyr package deal in R to modify the column positions in an information body.

You’ll be able to virtue refer to modes to modify the column positions:

banner 468x60

Mode 1: Exit One Column to Entrance

#travel 'x' column to entrance
df %>% relocate(x)

Mode 2: Exit A number of Columns to Entrance

#travel 'x' and 'y' columns to entrance
df %>% relocate(x, y)

Mode 3: Exit Column to Place Next Every other Column

#travel 'x' column to put then 'y' column
df %>% relocate(x, .then=y)

Mode 4: Exit Column to Place Sooner than Every other Column

#travel 'x' column to put prior to 'y' column
df %>% relocate(x, .prior to=y)

Please see examples display each and every form with refer to information body:

#form dataset
df <- information.body(group=c('A', 'A', 'A', 'B', 'B', 'C', 'C'),
                 issues=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

  group issues 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

Instance 1: Exit One Column to Entrance

Please see code presentations virtue the relocate() serve as to travel one column to the entrance:

#travel 'assists' column to entrance
df %>% relocate(assists)

  assists group issues 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

Instance 2: Exit A number of Columns to Entrance

Please see code presentations virtue the relocate() serve as to travel a couple of columns to the entrance:

#travel 'issues' and 'assists' to entrance
df %>% relocate(issues, assists)

  issues assists group 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

Instance 3: Exit Column to Place Next Every other Column

Please see code presentations virtue the relocate() serve as to travel one column to a selected place then any other column:

#travel 'group' column to then 'assists' column
df %>% relocate(group, .then=assists)

  issues assists group 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

Instance 4: Exit Column to Place Sooner than Every other Column

Please see code presentations virtue the relocate() serve as to travel one column to a selected place prior to any other column:

#travel 'group' column to prior to 'rebounds' column
df %>% relocate(group, .prior to=rebounds)

  issues assists group 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

Extra Assets

Please see tutorials provide an explanation for carry out alternative habitual purposes the use of dplyr:

The right way to Take away Rows The usage of dplyr
The right way to Organize Rows The usage of dplyr
The right way to Filter out via A couple of Situations The usage of dplyr

banner 336x280

Leave a Reply

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