Session 1-1
Session 1-1
Session 1-1
First, you install R from http://cran.r-project.org/ web site then you will need to install R-
Studio from https://rstudio.com/products/rstudio/download/ web site.
R-Studio is an integrated development environment (IDE) that allows you to interact with
R more readily.
R-Studio is similar to the standard R, but is considerably more user friendly. It has more
drop-down menus, windows with multiple tabs, and many customization options.
The first time you open R-Studio, you will see three windows. A forth window is hidden
by default, but can be opened by clicking the File drop-down menu, then New File, and
then R Script.
Console window - This is the window where you type in commands and the results
are returned.
Environment / History - This window shows all the objects that you have created
in the current R session (Environment tab) and the commands you have used in the
current R session (History tab).
Files / Plots / Packages / Help - This window is primarily used for displaying plots
(graphs) and for using the help system.
Code Editor - You can either type code directly into the Console window, or you
can type it into a text file (Code Editor), and then run the code you have written in
the Console window.
Before you begin working in R, you should set your working directory (a folder to hold all
of your project files). To change the working directory in R-Studio, select main menu
Session >> Set Working Directory >> Choose Directory.
Operators in R can mainly be classified into the following categories
Arithmetic operators
Relational operators
Logical operators
Assignment operators
In R, standard mathematical rules apply throughout and follow the usual left-to-right order
of operations: parentheses, exponents, multiplication, division, addition, subtraction
(PEMDAS).
console:
All the mathematical functions you could ever want are the table below.
Function Description
log to base e of x
antilog of x (ex)
log to base n of x
log to base 10 of x
square root of x
x!
greatest integer less than x (rounding down)
smallest integer greater than x (rounding up)
round the value of x to an integer (rounding to the nearest integer)
the absolute value of x, ignoring the minus sign if there is one
Exercise
Operator Description
Less than
Greater than
Less than or equal to
Greater than or equal to
Equal to
Not equal to
An example run
Logical operators are used to carry out Boolean operations like AND, OR etc.
An example run
These operators are used to assign values to variables.
Operator Description
Assignment
Objects are assigned values using (An equal sign, , can also be used.). For example,
the following command assigns the value 5 to the object x.
There are three important things to remember when selecting names for your variables in
R:
Variable names in R are case sensitive, so is not the same as .
Variable names should not begin with numbers (e.g. ) or symbols (e.g. ).
Variable names should not contain blank spaces (use not ).
Exercise
We can use the function to check the data type of a variable, for example