Basic Operations
Note: Today we will go through how to install R-Studio, use the console for basic math, and store expressions and calculations as variables..
Installing and Navigating R and R-Studio
In order to get the confusion out of the way, when we are working in R-Studio, we are going to just call it R. There are two distinct programs, R and R-Studio. The differences is that R does not have a Graphical User Interface. This means that there are no dialog boxes or viewer panes built in. I prefer using R-Studio because it alots a few more options to the user.
You will first need to install R. Make sure to note the operating system you are working on when doing so.
Once R is installed, you will need to install R-Studio.
When you open R you will see that there are three large boxes on the screen. The largest box on the left is the
Console
. This is where whatever you type will be evaluated by the program and when you pressEnter
, a result will appear below what you have typed.
Try it out: Type your name into the console and press Enter. Do you notice that you get an error message? Why do you think this is?
- The area on the right is divided into two parts: A tabset with the options;
Environment
,History
,Connections
, andBuild
. Additionally, there is a tabset directly below with the options;Files
,Plots
,Packages
,Help
, andViewer
.
For the moment, we will only be paying attention to the Console
.
Basic Math
It may seem trivial at first, but it is good to know that the Console
in R has an interface that acts as a calculator. The green text is what is typed into the console, and the black text, preceded by [1] is the result of the expression.
R will also follow the order of expressions, or how must of us know it: PEMDAS (Parentheses, exponents, multiplication, division, addition, and subtraction). This is especially important if you ever do a long formula.
Variables
Any expression you type into the console can be stored as a variable, which can be called later on. These variables can also be used in conjunction with different math expressions. It is important to note that when creating a variable, the variable can be used interchangably with the number(s) stored inside of it.
Note that the way we assign a number or object to a variable is by using <-
. You can also use =
, but as you will see in later seminars, the former option is a little more powerful.
You can use the shortcut: Option + Minus (Mac) | Alt + Minus (PC)
Try it out: Create a variable called Name
and have this equal your name. After you have done, type the variable Name
and press enter.
You most likley got an error message. Here is why…
Data Types
In R, there are different data types or classes that R will recognize. We will first focus on Numeric
and Character
. Take note of the class()
function that identify how R
evalutes this variable.
Now that we know that R
evaluates different types of data in different ways, let us try out the Name exercise again.
Perfect!
The reason that it would not work before is because R
has no way of knowing what you are trying to type unless you explicitly tell it that you are trying to type a name, which is a character, denoted by two quotation marks.
Try it out: Create a variable with the following numbers: 1,1,2,3,5,8,13 and store it under the name Fib
. Type Fib
back into the console and press Enter
.
You probably were greeted with another error message. This is because R
has no way of knowing that you want to display 7 distinct numbers. To get around this we have to do something called “concatenation”.
If we want to create a variable with more than one number, or object, inside of it we need to concatenate or combine that data first. In R
this is done using the c()
function. We can also call an object containing several unique numbers, a vector
.
By using the c()
function, we are telling R
to take these numbers (separated by a comma) and combine them so that they are all included in the variable, but still have their individuality.
If you wanted to see what the 5th number was in the Fib
, you would put a bracket []
next to the variable name. Inside of the brackets you would put the position you wanted to see.
Try it out: Create a numeric vector named Add3
containing the numbers, 1,4,6,9. How could we go about adding 3 to each number in this vector?
It was mentioned before that the variables take on the properties of the objects in which they are created from. What this means is that if I declare x <- 3
, x + 3
, my result should be 6. With this in mind, the same should work on a group of numbers.
This is especially helpful when you are dealing with data that you need to transform. We will learn about data transformation in later seminars.