By no means search for tidyr’s pivot_wider or pivot_longer once more!

Many tidyverse customers flip to the tidyr R bundle for reshaping information. However I’ve seen folks say they’ll’t bear in mind precisely how its pivot_wider() and pivot_longer() features work. Fortunately, there’s a simple resolution: RStudio code snippets. Write a snippet as soon as, and what’s principally a fill-in-the-blank kind will all the time be at your fingertips.

From vast to lengthy

To go from vast to lengthy with tidyr, use the pivot_longer() operate. It has the next syntax:

pivot_longer(mydata, 
cols, # columns that ought to pivot from vast to lengthy (unquoted)
names_to, # identify of the brand new class column as a quoted string
values_to # identify of the brand new worth column as a quoted string
)

For a easy instance, let’s take a look at the well-known mtcars information set, which has a large format. It doesn’t have a column class in any respect — fashions are row names however not in their very own variable — so I used the tibble bundle’s useful rownames_to_column() operate so as to add a Mannequin column from the row names:

library(tibble)
library(tidyr)
mtcars <- tibble::rownames_to_column(mtcars, "Mannequin")
head(mtcars)
              Mannequin  mpg cyl disp  hp drat    wt  qsec vs am gear carb
1         Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
2     Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
3        Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
4    Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
5 Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
6           Valiant 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

To transform mtcars into “tidy” or lengthy format, all of the columns ranging from mpg to the final one (carb) ought to shift into simply two new columns — one for class and one other for worth:

mtcars_long <- pivot_longer(mtcars,
cols = mpg:carb,
names_to = "Class",
values_to = "Worth"
)

Beneath is code for a reusable RStudio code snippet that generates a fill-in-the-blank code explainer for pivot_longer() everytime you invoke it. This isn’t R code; it’s snippet code to generate R code:

snippet plonger
pivot_longer(${1:mydf},
cols = ${2:columns to pivot lengthy},
names_to = "${3:desired identify for class column}",
values_to = "${4:desired identify for worth column}"
)

All of the objects enclosed with ${} are snippet variables. In the event you don’t understand how RStudio code snippets work, you’ll be able to watch this tutorial video:

You possibly can add the snippets code to your RStudio R code snippets file with the next R command:

usethis::edit_rstudio_snippets()

That opens the snippets file inside RStudio. You possibly can then copy and paste — or sort — code into that file. Observe that each line below the primary line throughout the snippet and the identify of the snippet (plonger) should begin with a tab.

Dropdown list with the name of a code snippet when starting to type the snippet name Sharon Machlis, IDG

Start typing the name of a snippet to select and use it.

To use the snippet, start typing plonger and the snippet name will appear as a choice to select and use. Watch the video embedded at the very top of this article if you want to see the pivot_longer() snippet in action. 

Added bonus: The snippet already includes quotation marks where they’re needed, so you don’t have to add them.

From long to wide

To reshape the other way, use pivot_wider():

pivot_wider(mydata, 
id_cols, # optional vector of columns you do not want affected
names_from, # category column(s) to pivot from long to wide
values_from # value columns(s) that hold data for each category column
names_sep # optional string separator for category-value columns
)

For this demo I’ll use the us_rent_income data frame, which has data by US state for median annual income and median monthly rent, plus margins of error for each.

  GEOID NAME    variable estimate   moe
  <chr> <chr>   <chr>       <dbl> <dbl>
1 01    Alabama income      24476   136
2 01    Alabama rent          747     3
3 02    Alaska  income      32940   508
4 02    Alaska  rent         1200    13
5 04    Arizona income      27517   148
6 04    Arizona rent          972     4

For a more human-readable version, I’d want income and rent to each have their own columns: income, income margin of error, rent, and rent margin of error. 

Code for that:

rent_income_wide <- pivot_wider(us_rent_income,
# id_cols = optional vector of unaffected columns,
names_from = c(variable),
values_from = c(estimate, moe),
names_sep = "_"
)

The RStudio code snippet below will generate reusable fill-in-the-blank code and explanations for each pivot_wider() argument.

snippet pwider
pivot_wider(${1:mydf},
# id_cols = ${2:optional vector of unaffected columns},
names_from = c(${3:category column(s) to pivot wide}),
values_from = c(${4:value column(s) that hold data for each category column}),
names_sep = "_"
)

You can download a text file with both pivot snippets below. Right-click and save the link as a file on your system; don’t copy and paste from the file displayed in a browser.

For more R tips, head to the InfoWorld Do More With R page.

Copyright © 2021 IDG Communications, Inc.

Source link

Leave a Reply