Get startedGet started for free

Providing relocation assistance

1. Providing relocation assistance

Welcome to Chapter 2. In this chapter, we'll move columns in our data, perform the same transformation across multiple columns, and choose rows that match column criteria. To start, we'll explore rearranging columns with a variety of tidyselect functions, which is useful for many reasons, such as comparing values across columns quickly when viewing our data.

2. The column names of world_bank_data

First, let's review the order of the columns in world_bank_data via the names function. Note that the columns starting with "perc" do not fall together in the current ordering.

3. Moving with select()

The goal is to move the columns starting with "perc" to just after the year column and leave the other columns in the same order. Let's walk through this in three steps, assigning our new tibble to reordered_wb. First, select the columns iso through year as they currently are in world_bank_data using the colon. Second, use the matches function with caret-perc to choose all columns that begin with perc. Third, the everything function tells select that we want all other remaining columns not covered by the first two steps to appear. The columns now appear in reordered_wb in the order specified.

4. Using last_col() instead

last_col is another helper function for ordering columns. As the function name suggests, it will return the last column in the data. Thus, we needn't scroll through a long vector of column names to find the name of the last column. Let's repeat the same reordering, but this time, using last_col instead of everything. Now we are choosing all columns from infant_mortality_rate, which is the first non-perc column name, to whatever the last column is in our data using the last_col function. We, again, use a colon between infant_mortality_rate and last_col to select a range of columns. Outputting the column names, we can see they are the same as before.

5. A simpler way with relocate() and .after

The select function can be helpful if we'd like to be explicit about where all of the columns will go in our reordering. But it can also be a little cumbersome to lay out the columns, especially if we just want to move a few columns around. Let's again work to put these perc columns together, just after the year column. This time we'll use the relocate function from the dplyr package. The first argument is which column or columns to move. In this case, we will move all columns starting with perc. The dot-after argument accepts the column we'd like the relocation to occur after. In this case, we relocate the perc starting columns to after the year column.

6. relocate() with .before

Instead of dot-after, we can use dot-before to move a column or columns to be before the specified column. Here, the specified column is infant_mortality_rate, so all of the columns ending in perc will be moved to before infant_mortality_rate. We again see the same results here in terms of column ordering.

7. Let's practice!

Do some rearranging using these tools on the IMF data in the exercises. Go get 'em!