With great power comes not only great responsibility, but often great complexity — and that sure can be the case with R. The open-source R Project for Statistical Computing, a programming language and environment, offers immense capabilities to investigate, manipulate and analyze data. But because of its sometimes complicated syntax, beginners may find it challenging to improve their skills after learning some basics.
If you’re not even at the stage where you feel comfortable doing rudimentary tasks in R, we recommend you head right over to Computerworld’s Beginner’s Guide to R. But if you’ve got some basics down and want to take another step in your R skills development — or just want to see how to do one of these four tasks in R — please read on.
I’ve created a sample data set with three years of revenue and profit data from Apple, Google and Microsoft, looking at how the companies performed shortly after the 2008-09 “Great Recession.” (The source of the data was the companies themselves; “fy” means fiscal year.) If you’d like to follow along, you can type (or copy and paste) this into your R terminal window:
The code above will create a data frame like the one below, stored in a variable named “companiesData”:
(R adds its own row numbers if you don’t include row names.)
If you run the str() function on the data frame to see its structure, you’ll see that the year is being treated as a number and not as a year or factor:
I may want to group my data by year, but don’t think I’m going to be doing specific time-based analysis, so I’ll turn the fy column of numbers into a column that contains R categories (called factors) instead of dates with the following command:
companiesData$fy <- factor(companiesData$fy, ordered = TRUE)
Throughout the course of this tutorial, I’ll also show how to accomplish these tasks using packages in the so-called “tidyverse” — an ecosystem initially championed by RStudio Chief Scientist Hadley Wickham and now backed by a number of open-source authors both within and outside of RStudio.
For creating ordered factors, the tidyverse forcats package has several options, including
companiesData$fy <- forcats::as_factor(as.character(companiesData$fy)).
Now we’re ready to get to work.
One of the easiest tasks to perform in R is adding a new column to a data frame based on one or more other columns. You might want to add up several of your existing columns, find an average or otherwise calculate some “result” from existing data in each row.
There are many ways to do this in R. Some will seem overly complicated for this easy task at hand, but for now you’ll have to take my word for it that some more complex options can sometimes come in handy for advanced users with more robust needs. However, if you’re looking for an easy, elegant way to do this now, skip to Syntax 5 and the dplyr package.
Simply create a variable name for the new column and pass in a calculation formula as its value if, for example, you want a new column that’s the sum of two existing columns:
dataFrame$newColumn <- dataFrame$oldColumn1 + dataFrame$oldColumn2
As you can probably guess, this creates a new column called “newColumn” with the sum of oldColumn1 + oldColumn2 in each row.
For our sample data frame called data, we could add a column for profit margin by dividing profit by revenue and then multiplying by 100:
companiesData$margin <- (companiesData$profit / companiesData$revenue) * 100
That gives us:
Whoa — that’s a lot of decimal places in the new margin column.
We can round that off to just one decimal place with the round() function; round() takes the format:
round(number(s) to be rounded, how many decimal places you want)
So, to round the margin column to one decimal place:
companiesData$margin <- round(companiesData$margin, 1)
And you’ll get this result: