4 data wrangling tasks in R for advanced beginners
If you don't want to keep typing the name of the data frame followed by the dollar sign for each of the column names, R's with() function takes the name of a data frame as the first argument and then lets you leave it off in subsequent arguments in one command:
companiesOrdered <- companiesData[with(companiesData, order(fy, -margin)),]
While this does save typing, it can make your code somewhat less readable, especially for less experienced R users.
Packages offer some more elegant sorting options. The doBy package features orderBy() using the syntax
orderBy(~columnName + secondColumnName, data=dataFrameName)
The ~ at the beginning just means "by" (as in "order by this"). If you want to order by descending, just put a minus sign after the tilde and before the column name. This also orders the data frame:
companiesOrdered <- orderBy(~-margin, companiesData)
Both plyr and dplyr have an arrange() function with the syntax
arrange(dataFrameName, columnName, secondColumnName)
To sort descending, use desc(columnName))
companiesOrdered <- arrange(companiesData, desc(margin))
Reshaping: Wide to long (and back)
Different analysis tools in R -- including some graphing packages -- require data in specific formats. One of the most common -- and important -- tasks in R data manipulation is switching between "wide" and "long" formats in order to use a desired analysis or graphics function. For example, it is usually easier to visualize data using the popular ggplot2() graphing package if it's in long format. Wide means that you've got multiple measurement columns across each row, like we've got here:
Each row includes a column for revenue, for profit and, after some calculations above, profit margin.
Long means that there's only one measurement per row. but likely multiple categories, as you see below:
Please trust me on this (I discovered it the hard way): Once you thoroughly understand the concept of wide to long, actually doing it in R becomes much easier.
If you find it confusing to figure out what's a category and what's a measurement, here's some advice: Don't pay too much attention to definitions that say long data frames should contain only one "value" in each row. Why? For people with experience programming in other languages, pretty much everything seems like a "value." If the year equals 2011 and the company equals Google, isn't 2011 your value for year and Google your value for company?
For data reshaping, though, the term "value" is being used a bit differently.
I like to think of a "long" data frame as having only one "measurement that would make sense to plot on its own" per row. In the case of these financial results, would it make sense to plot that the year changed from 2010 to 2011 to 2012? No, because the year is a category I set up in advance to decide what measurements I want to look at.
Even if I'd broken down the financial results by quarter -- and quarters 1, 2, 3 and 4 certainly look like numbers and thus "values" -- it wouldn't make sense to plot the quarter changing from 1 to 2 to 3 to 4 and back again as a "value" on its own. Quarter is a category -- a factor in R -- that you might want to group data by. However, it's not a measurement you would want to plot by itself.
- Software Asset Management: Ensuring Today's Assets Today's trends like BYOD and SaaS are new and exciting in terms of how they will help make our jobs more productive but...
- Trends Shaping Software Management: 2014 Most IT executives recognize the relationship between mobile computing and worker productivity, and have long issued notebook computers and other mobile devices to...
- Software Asset Management: Pay Attention or Pay Up There is a wide range of options for managing software assets, from in-house solutions to the cloud to managed services providers. Read this...
- 13 Reasons to Move to Adobe Creative Cloud One of the big advantages Adobe Creative Cloud for teams offers over Adobe Creative Suite 6 perpetual software is the ability to continually...
- Capturing Data in Motion: Delivering Real-Time Insight from Data Streams This webcast will help organizations of all types and sizes learn about a technology and business strategy for tapping into the wealth of...
- The Next Generation of Big Data: New IBM Information Management Cloud Solutions Learn about IBM's new and expanded Information Management capabilities now delivered in the cloud, including: Hadoop based analytics, stream processing, in-memory computing, data... All Business Intelligence/Analytics White Papers | Webcasts
Our new bimonthly Internet of Things newsletter helps you keep pace with the rapidly evolving technologies, trends and developments related to the IoT. Subscribe now and stay up to date!