0% found this document useful (0 votes)
3 views24 pages

Note 2 - Intro To R

The document provides an introduction to R, a free programming language for statistical computing and graphics, highlighting its capabilities in data analysis, modeling, and graphical representation. It covers basic operations such as starting and exiting R, managing the working directory, using variables, and performing calculations with vectors. Additionally, it explains how to access help, manage command history, and handle missing values.

Uploaded by

cinawe7046
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views24 pages

Note 2 - Intro To R

The document provides an introduction to R, a free programming language for statistical computing and graphics, highlighting its capabilities in data analysis, modeling, and graphical representation. It covers basic operations such as starting and exiting R, managing the working directory, using variables, and performing calculations with vectors. Additionally, it explains how to access help, manage command history, and handle missing values.

Uploaded by

cinawe7046
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NOTE 2: INTRODUCTION TO R

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 1


• R is a language and environment for statistical computing and graphics.
• R is available as a FREE software and runs on Linux, Windows and Macintosh.
• R provides a wide variety of statistical analysis;
o Linear and nonlinear modelling
o Classical statistical tests
o Time – series analysis
o Classification
o Clustering
o Big data analysis
o Data mining
o Graphical representation towards data

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 2


• R is;
✓ an effective data handling and storage facility.
✓ a suite of operators for calculation on arrays, in particular matrices.
✓ a large, coherent, integrated collection of intermediate tools for data analysis.
✓ a well-developed, simple and effective programming language which
includes conditionals, loops, user-defined recursive functions and input and
output facilities.

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 3


Starting and Exiting R
• To start,
✓ Double click on the R shortcut icon

• To exit,
✓ On the command window, type : >q()
✓ Menu command: File → Exit

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 4


• R is code driven.
• It fundamentally follows the question and answer model.
• R issues a prompt when it expects input commands. The default
prompt is “ >”.
• If a command is too long to fit on a line, a “+” is used for the
continuation prompt.
• R is case sensitive.

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 5


Working directory
• To print the current working directory, type: >getwd()

• To change the current working directory, type: >setwd(“H:”)


→ specify the path in which you want to save your project.
Ex: “C:/Users/S20999/Desktop/STA1031”

The Workspace
• Your R objects are stored in a workspace.
• To list the objects in your workspace, type: >ls()

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 6


To save the workspace to the file
“[Link]”
• Default file type: “ .Rdata”
>[Link]()
• To load a workspace into the current session:
>load(“[Link]”) OR
>load(“H:/mydocuments/[Link]”)
• Including file paths

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 7


History
• To work with your previous commands:
>history() → display last 25 commands
>history([Link]=Inf) → display all previous commands

• To save your command history default name as “ [Link] ”


>savehistory(file=“myfile”)

• To recall your command history:


>loadhistory(file=“myfile”)

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 8


Getting Help
• If you know a particular command, but don’t know the correct
syntax then:
help(“command”)
Ex: >help(“ls”) OR >?ls

• If you don’t know the command, but know the keyword then:
[Link](“key word”)
Ex: >[Link](“standard deviation”)

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 9


• If you want a list of all the functions containing a particular word then:
apropos(“word”)
Ex: >apropos(“read”)

• If you want to get an example of how a function is used then:


example(“function_name”)
Ex: >example(“[Link]”)

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 10


R as a calculator
• Oneof the simplest possible tasks in R is to enter an arithmetic expression
and obtain a result.

The number in the brackets is the


index of the element of the output.

To find the square root of a number


Eg: 16

To find the exponent of a number


Eg: e-2
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 11
To find the value of 𝜋

To find natural logarithm of 100 (with base ‘e’)

To find the logarithm of 100 with base 10

To find 122 ; base^exponent

To calculate 3! = 1 × 2 × 3

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 12


This code doesn’t give you the correct result,
because R always works with angles in
radians, not in degrees

The correct way to calculate the sine of


an angle of 120 degrees

Important:
R is CASE SENSITIVE!!!

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 13


Variables
• Even on a calculator, it is necessary to store intermediate results. For
that purpose R, like other computer languages, has symbolic variables.
(Names you can use to represent values).

• There are three types of variables;


1. Numeric
Ex: 2, 10092, 4.566778
2. Character
Ex: ‘Silver’, ‘Statistics’, ‘Computer’
3. Logical
Ex: TRUE, FALSE

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 14


Naming Variables
• Variables names
✓ can be built using letters, digits, period (.) and underscore ( _ ).
✓ cannot contain multiple words. If there are multiple words, they can be
connected using the underscore or period sign.
✓ must not start with a digit or a period followed by a digit. that start with a
period are special and should be avoided.
✓ are case-sensitive.

• Some names are already used by the system. You cannot use the following as
variable names.
Ex: c, q, t, D, F, I, T, diff, df, pt

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 15


Assigning values to variables
• “<-” , “->” and “=” signs are used to indicate assignment.
> x<-5 OR > 5->x OR > x=5

• To display the value,


> x
[1] 5

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 16


Vectors
• You cannot do much statistics on single [Link] we want to work with more
than one number, the solution is VECTORS.
• A vector is a sequence of data elements of the same basic type.
• One advantage of R is that it can handle entire data vectors as single objects.

c(……….) means concatenating

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 17


Example 1:
Enter the following numbers into X and perform the following operations.
3, 5, 7, 2, 6, 8, 4, 1, 9, 10
i. a=X+X
ii. b = X2/4
iii. y = a + b + 2
iv. z = 1/y
v. Print X, a, b, y and z

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 18


Answer

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 19


• Suppose you want to recover the 3rd element of the vector X in Example 1.
The index of the corresponding element should
indicate within square brackets

• You can also view the first 3 elements and the last 2 elements separately.

To display the first 3 elements

To display the last 2 elements

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 20


Character Vectors
• A character vector is a vector of text strings, whose elements
needs to be specified and printed within quotes.

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 21


Logical Vectors
• Logical vectors can take the value TRUE or FALSE
• In inputting, you may use the convenient abbreviation T or F
>c(T,F,T,F,F,T)

Ex: (Using the X vector from example 1)

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 22


Missing Values
• Missing values are represented by the symbol NA (Not Available).
• Impossible values (e.g. Dividing by zero) are represented by the symbol "Nan"
(Not A Number).
• Infinite values are represented by "Inf“.

STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 23


Example 2
Enter the following numbers into X and Y respectively and perform the
following operations and print the outputs.
X → 3, 6, 1, 8, 10 Y →5, 7, 4, 2, 9

i. a=X+Y
ii. Find a2 and assign the values to a vector b
iii. Create a logical vector Z to check if X is greater than Y
iv. Print the first three elements of Z
v. Multiply the first two elements of X with the last two elements of Y
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 24

You might also like