R Statistical Analysis for Beginners

In this guide, I will help you learn R, the statistical programming language.

Table of Contents

Chapter 1

R is a programming language that can do many things. Visualizations, data processing, and manipulation of data are all easy for R. You can download R from r-project.org or from a package manager that you may use. It usually goes by the same name. It is maintained very well and has a lot of active users. Being free is also a major advantage. It runs on Windows, Mac, and Linux. 

You do not use a compiler with R code. It is an interpreted language. Every statement is interpreted on a low level basis. However, interpreted languages are slower than those that use a compiler. This won’t make any difference at the beginning. Just keep it in mind if you decide to use it for more complicated tasks. It is not meant to be C++ as it does different things. 

 The R Environment

We can use any text editor and many different IDE’s to write R code. Depending on your operating system, you will have a few different choices. Some of those include:

  • RGui
  • RStudio
  • Eclipse
  • Emacs
  • Tinn-R
  • R Commander     
  • Rattle for R

There are many more options to go with. These are some of the more well known that are free. 

Entering Commands

You can use R as a simple calculator if you want to. Enter an expression and hit enter.

3 + 5

4^2

After you enter your expression and hit enter, the interpreter returns an answer.  To close your session, type:

quit()

Your First Session

Our first session will complete the requisite “Hello World” program. 

print (“Hello World”)

That is nice and easy, it only takes one line. Basic math is just as easy.

3^3 + 4^4

You should get the answer of 283. However, this is just the most basic usage and not really helpful. 

Starting With Vectors

A vector is a collection of the same type of things. If there are just numbers in the collection, it is called a numeric vector. To create a vector of numbers, you can type:

c(1,2,3,4,5,6,7)

This will create a vector of the numbers one through seven. 

You can also create a vector with the sequence operator, a colon.

1:100

This expression creates a vector of numbers from one to a hundred. That is much quicker than typing them out. You can add all of them together by using the sum function.

sum(1:100)

Variables and Vectors

We can store numbers and vectors in variables. This lets us store values to work on them at a later point. When they are stored, we can do calculations on them at any time. 

x = 1:100
x

The above statement assigns a vector to the variable “x”. Then we print the value of “x” with the next statement. We can create another variable and add it to the value of “x”.

a = 6^2

x + a 

Interaction With User

 We can get feedback from a user. To do this, we use the “readline()” function. 

color = readline("What is your favorite color? ")

paste (color)

Your First Script

Anytime you want to repeat some action, you should place the commands in a script. A script runs several commands at once. A script can run several calculations or ask for input. The only limit is your creativity.  

To create a script, you need to open a script window. In Rstudio:

  • In the top menu, click File
  • Select New File
  • Click R Script

This will open a window in the top left. You can paste the previous code into this window if you want. Save the file in the location that you prefer. Then, in the menu:

  • Select Code
  • Then click on Source towards the bottom

This will run the code in your bottom left window.