4 data wrangling tasks in R for advanced beginners

Learn how to add columns, get summaries, sort your results and reshape your data.

Pivoting long to wide

As the name implies, tidyr's pivot_wider() function turns a "long" data frame into a wider one. Before using it, think briefly about the structure you want to create. The names_from argument is the column(s) you want to change -- where each value should be turned into its own separate column. The variables that you want to remain as their own columns -- as row identifiers -- are your ID columns. They usually don't need to be specified, since the pivoting columns are already spelled out.

Look at this row from the original, "wide" version of our table:

fy company revenue profit margin
2010 Apple 65225 14013 21.5

Everything following fiscal year and company is a measurement relating to that specific year and company. fy and company are the ID variables; while revenue, profit and margin are the "names_from" variables that have been "pivoted" to become column headers.

How to re-create a wide data frame from the long version of the data? Here's code, if you've got one column with values that you want to turn into their own columns:

wideDataFrame <- pivot_wider(longDataFrame, names_from = column_where_each_value_becomes_a_new_column, values_from=column_with_measurement_values)

So, to produce the original, wide data frame from companiesLong using pivot_wider():

companiesWide <- pivot_wider(companiesLong, names_from = variable, values_from = value)

To break that down piece by piece: companiesLong is the name of my long data frame; I want to create a new column for each of the different categories in the variable column ; I want the actual measurements for each of those financial categories to come from the value column; and I don't need to specify fy and company because they are remaining as is as items in each row of my new, wide data frame.

Wrap-up

Hopefully instructions for these data-wrangling tasks have helped you solve a particular problem and/or continue on the journey of mastering R for data work. To learn more about R, see Computerworld's 60+ R resources to improve your data skills.

Find this article useful? You can download it as a free PDF or as part of our Advanced Beginner's Guide to R.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

More about AdvancedAppleGitHubGoogleMicrosoft

Show Comments
[]