What is R?
R is an extremely powerful statistical & data analysis tool that can help you do your work more efficiently, enable you to perform advanced analyses for a large number of applications, and the new skills will impress your boss and colleagues! R is already widely-used in academic and business circles, and is increasingly being used by analysts within the fundraising world.
In this, the first in a series of posts, we will introduce you to the world of R by exploring the basic data structures and core functionality of the software. The posts will all contain example R code using datasets that are available to anyone so that you can follow along at home. Future posts will build on this foundation as we explore more and more advanced topics – so stay tuned!
What Can I Do with R?
Answer: Just about anything you can dream up.
At its core, R is a statistical computing language that was built to handle a wide array of statistical analyses. However, one of its best features is that it’s Open Source, meaning anyone with the right know-how can create an add-on to the base software. These add-ons are known as Packages, and they allow for infinite customization and expansion of R’s core statistical functions. There are literally thousands of packages available for download, covering everything from advanced visualizations, to cutting edge machince learning techniques.
This ease of customization has drawn in an active & dedicated community of users spanning academic research and corporate data analysis fields who apply R’s powerful capabilities to a wide array of quantitative analyses, text mining, data visualization, and predictive modeling applications.
How Do Get Started?
All you need to do is follow these simple steps:
- Download and Install R from the CRAN website.
- The “base” R Console is pretty bare bones, so it’s recommended to add a Graphical User Interface (GUI) wrapper to improve the user experience and make R a bit easier to navigate. RStudio is a very popular option for this. (This and future R-related posts will use RStudio for all examples).
Once you’ve got RStudio up and running, (that little blinking cursor at the bottom of the Console next to the “>” means that R is ready & waiting for your first command!) you have the option to begin typing R code directly into the “Console” to be run immediately, or to a “Script” file. A Script is just a text file that allows you to save your R code and run it whenever you wish (now or 2 months from now). You can think of running code from the console as giving a speech from memory without any notes, and running code from a Script as giving that speech by reading from a … Script.
- Lines of code can be executed directly from the console by typing Return or Enter;
- If using a Script, code can be executed by highlighting the section you want and clicking the Run button in the top right corner of the Script window (or by typing Ctrl + Enter to run the entire line of code your cursor is on).
At the beginning of each R session, it’s good to get in the habit of setting what’s known as the Working Directory. This is the folder R will reference any time it needs to open or save anything on your computer. By default, R uses your home directory (for example, “My Documents” on Windows machines), but you can specify any folder you desire (the Working Directory can also be changed at any time).
To set your Working Directory, execute the following code from the console or a script:
Where the full file path to the Working Directory folder you want to use appears inside the parenthesis and inside the quotes.
Your actual code might look something like this:
At any time, you can check to see the Working Directory R is using by typing:
Now that you have downloaded & installed R and RStudio, and have set your Working Directory, we can begin to explore the power of R!
R is able to perform simple arithmetic, just like a calculator:
2 + 3
##  5
10 * 10
##  100
100 ^ 2 # The "^" symbol is used for exponentiation in R
##  10000
7 + 3 - 5 * 2 # Notice that R automatically recognizes the correct order of operations
##  0
Also note in the code above, the “#” allows us to make comments on a line of code. This can be a very helpful way of creating notes or reminders.
Logical expressions can also be evaluated:
5 > 2
##  TRUE
5 == 5.01 # Two consecutive equals signs ("==") stand for "exactly equal to" in R
##  FALSE
5 != 5.01 # The "!=" means "NOT equal to"
##  TRUE
Other helpful functions:
There are a whole raft of other “basic” functions that R handle, examples include:
log(100) # computes the Logarithm of 100
##  4.60517
sqrt(100) # computes the Square-Root of 100
##  10
exp(100) # computes the Exponential function of 100
##  2.688117e+43
cos(100) # computes the Cosine of 100
##  0.8623189
Understanding the Structure of Data in R
Data Classes & Modes
In R, every Object, belongs to a certain data Class and each Class has its own unique properties called “modes,” that govern how an object will behave or interact with other objects.
R’s data Classes are:
In addition, a few of the most common Modes include:
You will notice that Numeric, Logical, and Character can be both Classes and Modes.
Although it is neither a data Class nor a Mode, one of the most basic R Objects that you will encounter frequently are Vectors. Typically, a vector can only contain objects of the same class (for example, the sequence of numbers from 1-10 is a vector containing only numerics). However, there is a special type of vector called a List which can contain a mix of data classes.
One of the most useful aspects of R – and also potentially one of its most puzzling if you’re new to coding – is the ability to create new objects by assigning names to certain values or calculations. These new objects can then be used and manipulated in your analysis for any purpose you require.
We can assign a name to any object by creating an “arrow” with the ‘<’ and ‘-’ symbols (the RStudio shortcut for this on a Windows machine is Alt + –) For example:
a <- 1 + 1
creates a new object called ‘a’. You should be able to see this new object in the Global Environment pane of your RStudio session.
We can ask R to show us what our new object ‘a’ represents by simply typing:
##  2
or more formally, by using the print command:
##  2
Not surprisingly, the object ‘a’ represents the result of the equation 1 + 1.
We can further evaluate our new object by asking R to tell us its Class and Mode:
##  "numeric"
##  "numeric"
Because the object ‘a’ simply represents the number 2, both its Class and Mode are “numeric.”
As a final step for Part I of our series on Working with Data in R, let’s create a more complicated object, consisting of five numbers:
vec <- c(2, 4, 6, 8, 10)
In the code above, we’ve created a Vector of five numbers by using the c() function (which stands for “concatenate”), and assigned it a new name: ‘vec’.
As before, we can examine our new object using a few different commands:
##  "numeric"
##  "numeric"
str(vec) # now that we have a slightly more complicated object, we can use the "structure" command, str()
## num [1:5] 2 4 6 8 10
Now we’re really on a roll! There’s lots more to cover, so stay tuned for Part II of our series on Working with Data in R! In the meantime, go ahead and explore our object ‘vec’ (or even create your own object) using the following commands:
min() max() range() length() sum() mean() median() var() sd() summary()
To learn more about how the BWF Insight team uses analytics tools like R to help fundraisers all around the world, check us out on the web at BWF Insight.