Session 1-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

R is a flexible and powerful open-source implementation of the language S (for statistics)

developed by John Chambers and others at Bell Labs.


R is a free software environment for statistical computing and graphics, and it is widely
used among statisticians and data miners for developing statistical software and data
analysis.

R is an implementation of the S programming language. S was created by John Chambers


and R was created by Rose Ihaka and Robert Gentleman at the University of Auckland,
New Zealand.
R is currently developed by the R Development Core Team, of which Chambers is a
member.
R is named partly after the first names of the first two R authors and partly as a play on the
name of S.

R is open source and completely free. It is preferred program of many professional


statisticians and researchers in a variety of fields. R community members regularly

R is as good as (often better than) commercially available statistical packages like


SPSS, SAS, and Minitab.
R has extensive statistical and graphing capabilities. R provides hundreds of built-
in statistical functions as well as its own built-in programming language.
R is used in teaching and performing computational statistics. It is the language of
choice for many academics who teach computational statistics.
Getting help from the R user community is easy. There are readily available online
tutorials, data sets, and discussion forums about R.

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).

Operator Description Example


Addition
Subtraction
Multiplication
Division
Exponentiation
Remainder of x divided by y

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

1. Using R, verify that


6 a 42
29.50556
3 4.2 3.62
when a 2.3 .

2. Using R, how would you calculate the square root of half of


the average of the numbers 25.2, 15, 16.44, 15.3, and 18.6?

3. Find loge 0.3.

4. Compute the exponential transform of your answer to (3).


Relational operators are used to compare between values. Here is a list of relational
operators available in R.

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.

Operator Description Example Answer


not FALSE
or TRUE
and FALSE

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.

Another assignment to the same object will change the content.

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

1. Create an object that stores the value 32 × 41/8

2. Overwrite your object in (1) by itself divided by 2.33. Print the


result to the console.

3. Create a new object with the value -8.2 × 10-13

4. Print directly to the console the result of multiplying (2) by (3).


To see a listing of the objects in your workspace, you can use the function.
For example:

removes the objects indicated. For example, the object is removed by .

removes all the objects.

Basic data types in R can be divided into the following types

Data Type Example


Numeric 12.3, 5, 999
Character
Logical TRUE, FALSE, T, F

We can use the function to check the data type of a variable, for example

You might also like