R

Programming With R & RStudio®

 

There is the actual R program and then RStudio® which is a clean and simple user interface for R. All of the references to R and code will be made using RStudio, and not the R application. Before we jump into the coding of R it is important to do some house keeping tasks. Firstly, we use the Xcode formatting style because of it’s similarity to SQL and we are aspiring iOS app makers over here. Instructions on how to set this are available on the downloads page. Second, you’ll need to set up your working directory to ensure RStudio

Setup

You’ll first want to ensure that your working directory is good to go. So let’s check where your working directory is at right now. The following code will get you there.

##CHECK WORKING DIRECTORY
getwd()

Wait, why isn’t it capitalized and spelled “GetWD()”?
In R, capitalization matters, so our code will not be as pretty as our SQL lessons. However, the formatting makes sense and we show you when capitalization matters the most. We will strive to teach you the very best methods, so whomever reads your code will understand it and trust it.

Once this is entered we can then set our own working directory with the next chunk of code. Enter in your file path that you want your working directory to be in. The example follows what I use for work. Be careful to take note of the forward slash, you’ll have to replace the back slashes with a forward slash.

##SET YOUR WORKING DIRECTORY
setwd("T:/Folder1/Folder2/R/Directory")

Some people run into JAVA issues on 64 bit machines. We think this was corrected in an update but in case you encounter some JAVA issues, try running the following code.

##IF YOU HAVE JAVA ISSUES, USE THE FOLLOWING CODE TO FIX SOME ISSUES
Sys.setenv(JAVA_HOME='C:/Program Files/Java/jre1.8.0_66')

 

Now the lessons begin. R is strong for statistical analysis and creating amazing graphics to depict your analysis. And you may want to jump right into that, but first you need to understand the language and all the little quirks that come with it.

Lesson I - The Basics

Okay, on to some general knowledge.

R runs on packages, once a package is installed you can “library” it to use it. To see what libraries are already loaded run the following script.

##TO SEE WHAT LIBRARIES ARE LOADED
search()

A fundamental aspect of R is the idea of Objects, you can think of an object as tab of sorts in Excel, it houses information that can be used. It is not necessarily a data set or table, but information of many kinds. We will cover this later on. When setting something into an object use “<-” so R knows what you want to do. You are essentially inserting what you want into a named field or object.

##DONT USE AN EQUALS SIGN "=" INSTEAD USE "<-" 
NewObject <- file

##NOT
NewObject = file

Let’s go over some basic commands of R. See the scripts below and see if they make sense to you. You can run basic math and it will give you the answer in the console or you can set it into an object. Also, you can remove an object with the rm() function. See below.

##BASIC COMMANDS
2 + 2

Total <- 2 + 2

rm(Total)

The logical expressions in R follow:

##LOGICAL EXPRESSIONS
<, >, <=, >=, ==, !=, &, |(or), !(not)

Function Calls:

##FUNCTION CALLS
##COMBINE
c()

##REMOVE
rm()

##MEDIAN
median()

##MEAN
mean()

 

 

 

We are always adding and improving! Please check back soon for our updates!