4 data wrangling tasks in R for advanced beginners

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

1 2 3 4 5 6 7 8 Page 8
Page 8 of 8

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:

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.


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.

Copyright © 2019 IDG Communications, Inc.

1 2 3 4 5 6 7 8 Page 8
Page 8 of 8
It’s time to break the ChatGPT habit
Shop Tech Products at Amazon