Using R as a Calculator

This tech tip was provided by Gary R. Moser, Planning and Research Associate II, California State University, East Bay.

R is a powerful programming language and environment for data analysis that is well-suited to the wide variety of data manipulation and analytic tasks performed by IR professionals. However, getting started can be daunting. R works, looks, and feels quite different than SAS, SPSS, or Excel.

Most notably, R does not have a point-and-click user interface. Instead, one types commands at the prompt, and R performs the requested action. Commonly, users will save a series of commands to a simple text file to re-run at a later time; this type of script is comparable to SPSS and SAS scripts, or VBA for Excel. Such a keyboard-based interface has many advantages over point-and-click GUIs:

  • Repeatability: Work can be saved and re-used multiple times;
  • Documentation and Auditing: Your work serves as its own documentation that you or others can audit and reference; and
  • Safety: If you are a power user who relies on mouse clicks to get work done, there exists a serious danger of repetitive stress injury.

One of the best ways to fast-track your familiarity with R and quickly add a tool to your toolbox is to begin using it as a super-charged desktop calculator. This might seem trivial at first, but consider the following examples.

Typical arithmetic operations work as expected. When you are finished typing the expression at the prompt, hit Enter and the result is printed back to the R Console screen:

pic 1 R tech tip.png

Instead of having the results print back to the R Console, we can assign the results of an expression to object “my.object” using “<-”:

my.object <- 99

Or, you can use “=” to make assignments.

In advanced use there are reasons to prefer the arrow, but in general they are interchangeable - use the one you prefer:

my.object = 99

After that, we can type the name of the object at the prompt, hit Enter, and its value will print to screen. Further, when we work with “my.object” it will be replaced by the value it contains. There are several immediate benefits to this:
  • No need to type a value repeatedly (and possibly make mistakes);
  • No loss of precision by hand-entered numbers (only six (6) decimals are displayed in the example, but no rounding has actually occurred); and
  • Meaningful names can be used, such as “F10.Freshmen” making it easy to organize and keep track of multiple objects.
pic 2 R tech tip.png
The real power of this calculator becomes clearer once we begin to use objects and vectors. In the example below, the number of U.S. students enrolling in degree-granting institutions in 2010 (in thousands) by race/ethnicity is assigned to the object “f10.enrolled” [source: Digest of Education Statistics 2011, Table 237].
The “c” function “combines” several values into a single vector.
pic 3 R tech tip.png
Optional labels have been used above to help identify racial/ethnic groups. Without using labels, the assignment would be:
f10.enrolled <- c(12722.5, 3038.8, 2741.4, 1282.2, 196.4, 325.3, 709.6)

By typing the object name at the prompt and hitting Enter, we can see what “f10.enrolled” now contains. The labels help make output more readable, but the actual data held in this object are the numbers.
pic 4 R tech tip.png
From here, the advantage of working with objects and vectors can be seen. For example, let's find out how many total students there are using the “sum” function. Rather than write that number (21,016.2) down on a scrap of paper, let's assign it to an object named “total.f10.enrolled."
 pic 5 R tech tip.png
The object we defined earlier “f10.enrolled” contains enrollment counts for each racial/ethnic group. Let's find out what the group percentages are by dividing by the total. We are using a “vectorized” approach - that is, multiple calculations are being performed simultaneously with just one expression. This is one of R's features that makes it so flexible.
pic 6 R tech tip.png
Now you are only a few short steps away from performing statistical analysis on these data, or creating graphical summaries. For example:
barplot(f10.enrolled/total.f10.enrolled, main = "% Fall 2010 Enrolled by Race/Ethnicity", 
col = "lightsteelblue")
pic 7 R tech tip graph.png 
Many details have been excluded in this simplified example. As you apply this software to your data, I recommend consulting the following online resources:
You are also welcome to contact me directly with any questions you have. Happy computing!

 Tech Tip Comment Board

To add a comment, Sign In
Total Comments: 7
John posted on 9/24/2012 12:29 PM
AWESOME - someone who uses "R"!!!! Like SAS or SPSS, R is great for "scripting" analysis and then rerunning them when the data changes or is freshened. Plus - its FREE! I definitely recommend it for any hardcore data hounds!
Irene posted on 9/24/2012 2:56 PM
Nice introduction to R. R is a very accessible product with a large support community. Even if you are not hardcore, it's worth an exploration. Both money and time saving. Plus, R always remains at the forefront of statistical computing, offering processes (such as maximum likelihood estimation) long before they are addressed by SPSS.

If anyone is interested in exploring R through a class, a free MOOC (massive, open online course) started today (9/24/12) via Coursera called Computing for Data Analysis offered by Roger Peng of Johns Hopkins.
Norma posted on 9/24/2012 3:10 PM
Works well. As a person that has not worked in R before the screen shot for entering the f10.enrolled data confused me in that there is a “+” at the beginning prior to the prompt that is not visible on my screen. Where Does this come from? Otherwise works well and is something I will investigate in the future.
Shelia posted on 9/24/2012 3:13 PM
“R” seems like a great tool to avoid repetitiveness. Thanks and great job!
Gary posted on 9/24/2012 4:25 PM
Hi Norma, In order to keep the window for the screen shot small, I used a lot of carriage returns. When you are typing an expression in R and hit "Enter" before it is complete, R prompts you for the rest of the expression with "+". It's also a useful reminder when you think you've entered a complete expression but forgot a final parenthesis or quotation mark.
I appreciate the nice comments, and will check out the MOOC at Johns Hopkins!
Gary posted on 9/25/2012 3:47 PM
More of us will start using R when somebody writes a friendly interface. I thrash enough with SAS and data to take on R syntax too. Yeah, I know it's cool to be a coder, but really ...
Gary posted on 9/26/2012 4:46 PM
Gary, thanks for the comment. Yes, if one doesn't have much experience programming with SAS, SPSS, or something similar, learning R will be more challenging. The "crown jewel" of the software is it's powerful language; a GUI defeats the purpose. It's true that R will not hold your hand, but there are many great authors and website creators who will. If you choose to embark on the journey, you'll find a lot of great help out there. As for coding, whether its cool or not is second to the enormous benefits of using syntax, whether in SAS, SPSS, Stata, or R. Onecia's prior three tech tips illustrate that point well. Best of luck, whichever route you choose.