Saturday, January 3, 2015

Melting Columns to Rows in R

Pivoting data is a common issue when dealing with Excel or CSV input files.  Excel users generally like "dirty" yet presentable data.  Data dumps commonly place multiple tables worth of data into a single massive column table.  This style of multi-column data lends it self to problems when trying to analyze results using tools such as pivot tables.

For example, 12 months of column data should be converted to 2 columns (month name, value) for efficiently pivoting / filtering and sorting data from a Wide to Long format.

Excel can use some macro functionality to unpivot results, or an add-in like PrepYourData or Microsoft PowerQuery.
 
R provides this kind of functionality in 3 lines of code. In this case for subjects and scores across column to rows.

http://stackoverflow.com/questions/18446668/using-r-to-reformat-data-from-cross-tab-to-one-datum-per-line-format

http://seananderson.ca/2013/10/19/reshape.html

Tidy Data