SlideShare a Scribd company logo
1
R: THE TRUE BASICS
R, also called the Languagefor Statistical Computing, was developed by Ross
Ihaka and Robert Gentleman at the University of Auckland in the nineties. It is
considered an open sourceimplementation of the S language, which was
developed by John Chambers in the Bell Laboratories in the eighties.
R provides a wide variety of statistical techniques and visualization capabilities.
Another very importantfeature about R is that it is highly extensible. Because
of this and more importantly becauseR is open source, it actually was the
vehicle to bring the power of S to a larger community. Like in every
programming language, there are pros and cons.
ADVANTAGES:
1) It is an open sourceand free.
2) Master at graphics
3) Command – line Interface
4) Reproducibility through R scripts
5) R packages: Extensions of R
DISADVANTAGES:
1) Easy to learn, harder to master
2) Poorly written code hard to read/maintain
3) Command – Line interface daunting at first
4) Poorly written code is slow
The first step in R is one of the mostimportant components of R, and where
most of the action happens, is the R console. It's a place where you can
execute R commands. You simply type something at the promptin the console,
hit Enter, and R interprets and executes your command.
2
Let's start our experiments by having R do some basic arithmetic; we'll
calculate the sum of 1 and 2. We simply type 1 + 2 in the consoleand hit Enter.
R compiles what you typed, calculates the result and prints that resultas a
numerical value.
Now let's try to type sometext in the console. We usedouble quotes for this.
You can also simply type a number and hit Enter. R understood your character
string and numerical value, but simply printed that string as an output. This
sbrings meto the first super importantconcept in R: the variable. A variable
allows you to storea value or an object in R. You can then later use this
variable's name to easily access the value or the object that is stored within
this variable. You can use the less than sign followed by a dash to create a
variable.
3
Supposethe number 2 is the height of a rectangle. Let's assign this value 2 to a
variable height. We type height, less than sign, dash, 2: This time, R does not
print anything, because it assumes that you will be using this variable in the
future. If we now simply type and execute height in the console, R returns 2:
We can do a similar thing for the width of our imaginary rectangle. We assign
the value 4 to a variablewidth. If wetype width, we see that indeed, it
contains the value 4. As you'reassigning variables in the R console, you're
actually accumulating an R workspace. It's theplace where variables and
information is stored in R.
You can access the objects in the workspacewith the ls() function. Simply type
ls followed by empty parentheses and hit enter. This shows you a list of all the
variables you have created in the R session. If you havefollowed all the
examples up to now, you should see "height" and "width". This tells you that
there are two objects in your workspaceatthe moment. When you type height
in the console, R looks for the variable height in the workspace, finds it, and
prints the corresponding value. If, however, wetry to printa non-existing
variable, depth for example, R throws an error, becausedepth is not defined in
the workspaceand thus not found. The principle of accumulating a workspace
through variable assignmentmakes these variables available for further use.
Supposewe wantto find out the area of our imaginary rectangle, which is
height multiplied by width. Let's go ahead and type height asterisk width. The
result is 8, as you'd expect. We can take it one step further and also assign the
result of this calculation to a new variable, area. We again use the assignment
operator. If you now type area, you'll see that it contains 8 as well. Inspecting
4
the workspaceagain with ls, shows thatthe workspacecontains threeobjects
now: area, height and width.
5
Basic Data Types
R's fundamental data types, also called atomic vector types. Throughoutour
experiments, we will use the function class(). This is a usefulway to see what
type a variable is. Let's head over to the consoleand start with TRUE, in capital
letters. TRUE is a logical. That's also what class(TRUE) tells us. Logical are so-
called boolean values, and can be either `TRUE` or `FALSE`. Well, actually, `NA`,
to denote missing values, is also a logical.
We can performall sorts of operations on them such as addition, subtraction,
multiplication, division and many more. A special type of numeric is the
integer. Itis a way to represent natural numbers like 1 and 2. To specify that a
number is integer, you can add a capital L to them. We don't see the difference
between the integer 2 and the numeric 2 from the output. However, the
`class()` function reveals the difference. Instead of asking for the class of a
variable, you can also use the is-dot-functions to see whether variables are
actually of a certain type. To see if a variableis a numeric, we can usethe is-
dot-numeric function. Itappears that both are numeric.
To see if a variableis integer, we can use is-dot-integer. This shows us that
integers are numeric, but that not all numeric are integers, so there's some
6
kind of type hierarchy going on here. Lastbut not least, there's the character
string. The class of this type of object is "character".
It's importantto note that there are other data types in R, such as double for
higher precision numeric, complex for handling complex numbers, and raw to
storeraw bytes.
7
Vectors
1) Create and name vectors:
A vector is nothing more than a sequence of data elements of the _same_
basic data type.
Firstthings first: creating a vector in R! You use the `c()` function for this, which
allows us to combine values into a vector. Supposeyou'replaying a basic card
game, and record the suit of 5 cards you draw from a deck. A possibleoutcome
and corresponding vector to contain this information could be this one of
coursewe could also assign this character vector to a new variable, drawn suits
for example. We now have a character vector, drawn suits. Wecan assertthat
it is a vector, by typing is dot vector drawn suits
Likewise, we could create a vector of integers for example to storehow much
cards of each suit remain after we drew the 5 cards.
Let's call this vector remain. There are 11 more spades, 12 morehearts, 11
diamonds, and all 13 clubs still remain.
.
8
We can use the `names ()` function for this. Let's firstcreate another character
vector, `suits`, thatcontains the strings "spades", "hearts", "diamonds", and
"clubs", the names we wantto give your vector elements.
2) Vector Arithmetic:
We learned that we can usevariables to perform arithmetic Remember how
you summed apples and oranges? From the previous section, we also know
that actually these variables, `my_apples` and `my_oranges`,aresimply
vectors. This means that we can perform arithmetic with vectors in R.
9
The most important thing to remember about operations with vectors in R , is
that they will be applied element by element. This means that standard
mathematics is extended to vectors in an element-wise fashion.
Imagineyou have a vector containing your gambling earnings for the past 3
days. Not bad for a few days in the desert, is it? Imaginea well-dressed
gentleman approaches you and offers to triple your earnings for the past three
days, if you beat him in one round of poker. If you wantto calculate the
expected earnings for each of the pastthree days, you can easily do it in R.
As you can see, R multiplies each element in the `earnings` vector with 3,
resulting in 150 dollars of promised earnings in the first day, 300 in the second
day and 90 in the third day..
Likewise, division, subtraction, summation and many more are all carried out
element wise, just as if you are carrying outthe operation between two scalars
three times. Fromthese lines of code you don't see anything differentfrom
what we'vedone before, becauseof course, you were working with vectors all
along. The mathematics naturally extended to vectors that contain more than
one element. Let's go back to your Vegas adventures. To enjoy your earnings,
you also decided to go shopping and spend some money every day on the Las
Vegas Strip. You recorded a vector of expenses.
Because you are a very conscious programmer in training, you decide to
compute whether your luck in the casino was sufficient to pay for your
expenses.
10
MATRICES
Creating and naming matrices:
A matrix is kind of like the big brother of the vector. Where a vector is a
sequence of data elements, which is one-dimensional, a matrix is a similar
collection of data elements, but this time arranged into a fixed number of rows
and columns. Since we are only working with rows and columns, a matrix is
called two-dimensional.
The matrix can contain only one atomic vector type. This means that you can't
have logical and numeric in a matrix for example. There's really not much
more theory about matrices than this: it's really a natural extension of the
vector, going fromone to two dimensions. Of course, this has its implications
for manipulating and subsetting matrices, but let's start with simply creating
and naming them. To build a matrix, you usethe matrix function. Most
importantly, it needs a vector, containing the values you want to place in the
matrix, and at least one matrix dimension. You can chooseto specify the
number of rows or the number of columns. Havea look at the following
example, that creates a 2-by-3 matrixcontaining the values 1 to 6, by
specifying the vector and setting the row argument to 2: R sees that the input
vector has length 6 and that there haveto be two rows. Itthen infers that
you'll probably want3 columns, such that the number of matrix elements
matches the number of input vector elements.
11
If you prefer to fill up the matrix in a row-wisefashion, such
that the 1, 2 and 3 are in the first row, you can set the `by row` argumentof
matrix to `TRUE` Can you spot the difference? Remember how R did recycling
when you weresubsetting vectors using logical vectors? The same thing
happens when you pass the matrix function a vector that is too shortto fill up
the entire matrix. Supposeyou pass a vector containing the values 1 to 3 to the
matrix function, and explicitly say you wanta matrix with 2 rows and 3
columns: R fills up the matrix column by column and simply repeats the vector.
If you try to fill up the matrix with a vector whosemultiple does not nicely fit in
the matrix, for example when you want to put a 4-element vector in a 6-
element matrix, R generates a warning message.
Actually, apartfrom the `matrix()` function, there's yet another easy way to
create matrices that is more intuitive in some cases. You can pastevectors
together using the `cbind()` and `rbind()` functions. Havea look at these calls
`cbind()`, shortfor column bind, takes the vectors you pass it, and sticks them
together as if they were columns of a matrix. The `rbind()` function, shortfor
row bind, does the samething but takes the input as rows and makes a matrix
out of them. These functions can come in pretty handy, because they're often
more easy to use than the `matrix()` function.
12
If you want to add another row to it, containing the values 7, 8, 9, you could
simply run this
command: You can do a similar thing with `cbind()`: Next up is naming the
matrix. In the case of vectors, you simply used the names() function, but in the
case of matrices, you could assign names to both columns and rows. That's
why R came up with the rownames () and colnames () functions. Their use is
pretty straightforward. Retaking thematrix `m` from before,
we can set the row names justthe same way as wenamed vectors, but this
time with the row names function.

More Related Content

What's hot

[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
Muhammad Hammad Waseem
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
Rajeshkumar Reddy
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
Vince Vo
 
Lect 8(pointers) Zaheer Abbas
Lect 8(pointers) Zaheer AbbasLect 8(pointers) Zaheer Abbas
Lect 8(pointers) Zaheer Abbas
Information Technology Center
 
C# Strings
C# StringsC# Strings
C# Strings
Hock Leng PUAH
 
Handout # 6 pointers and recursion in c++
Handout # 6   pointers and recursion in c++Handout # 6   pointers and recursion in c++
Handout # 6 pointers and recursion in c++
NUST Stuff
 
C-Pubb-TestingExperience_Issue_08_2008
C-Pubb-TestingExperience_Issue_08_2008C-Pubb-TestingExperience_Issue_08_2008
C-Pubb-TestingExperience_Issue_08_2008
Berta Danilo
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
Mohammed Khan
 
Data Handling
Data HandlingData Handling
Data Handling
Praveen M Jigajinni
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In General
martha leon
 
Pointers
PointersPointers
Pointers
Prasadu Peddi
 
Python reference
Python referencePython reference
Python reference
Zeeshan Anjum
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
Dushmanta Nath
 
ppt on pointers
ppt on pointersppt on pointers
ppt on pointers
Riddhi Patel
 
Nn
NnNn
The Expression Problem - Part 1
The Expression Problem - Part 1The Expression Problem - Part 1
The Expression Problem - Part 1
Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Philip Schwarz
 
Basic python part 1
Basic python part 1Basic python part 1
Basic python part 1
National University of Malaysia
 
Monad Fact #4
Monad Fact #4Monad Fact #4
Monad Fact #4
Philip Schwarz
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
Ashim Lamichhane
 

What's hot (20)

[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
 
Lect 8(pointers) Zaheer Abbas
Lect 8(pointers) Zaheer AbbasLect 8(pointers) Zaheer Abbas
Lect 8(pointers) Zaheer Abbas
 
C# Strings
C# StringsC# Strings
C# Strings
 
Handout # 6 pointers and recursion in c++
Handout # 6   pointers and recursion in c++Handout # 6   pointers and recursion in c++
Handout # 6 pointers and recursion in c++
 
C-Pubb-TestingExperience_Issue_08_2008
C-Pubb-TestingExperience_Issue_08_2008C-Pubb-TestingExperience_Issue_08_2008
C-Pubb-TestingExperience_Issue_08_2008
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Data Handling
Data HandlingData Handling
Data Handling
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In General
 
Pointers
PointersPointers
Pointers
 
Python reference
Python referencePython reference
Python reference
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
ppt on pointers
ppt on pointersppt on pointers
ppt on pointers
 
Nn
NnNn
Nn
 
The Expression Problem - Part 1
The Expression Problem - Part 1The Expression Problem - Part 1
The Expression Problem - Part 1
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
 
Basic python part 1
Basic python part 1Basic python part 1
Basic python part 1
 
Monad Fact #4
Monad Fact #4Monad Fact #4
Monad Fact #4
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 

Similar to R Programming

Spreadsheets for developers
Spreadsheets for developersSpreadsheets for developers
Spreadsheets for developers
Felienne Hermans
 
c++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptxc++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptx
JanineCallangan
 
SessionFour_DataTypesandObjects
SessionFour_DataTypesandObjectsSessionFour_DataTypesandObjects
SessionFour_DataTypesandObjects
Hellen Gakuruh
 
Presentation of Linear Regression with Multiple Variable-4.pdf
Presentation of Linear Regression with Multiple Variable-4.pdfPresentation of Linear Regression with Multiple Variable-4.pdf
Presentation of Linear Regression with Multiple Variable-4.pdf
wahajshafiq455
 
Array
ArrayArray
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
KabilaArun
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
DrGSakthiGovindaraju
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
attalurilalitha
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in R
Rsquared Academy
 
1 1 number theory
1 1 number theory1 1 number theory
1 1 number theory
smillertx281
 
Numerical data.
Numerical data.Numerical data.
Numerical data.
Adewumi Ezekiel Adebayo
 
An applied approach to calculas
An applied approach to calculasAn applied approach to calculas
An applied approach to calculas
Tarun Gehlot
 
Matrix decomposition and_applications_to_nlp
Matrix decomposition and_applications_to_nlpMatrix decomposition and_applications_to_nlp
Matrix decomposition and_applications_to_nlp
ankit_ppt
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
VimalSangar1
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming
hccit
 
Learning R while exploring statistics
Learning R while exploring statisticsLearning R while exploring statistics
Learning R while exploring statistics
Dorothy Bishop
 
Unit 3
Unit 3Unit 3
Ggplot2 work
Ggplot2 workGgplot2 work
Ggplot2 work
ARUN DN
 
Introduction to R.pptx
Introduction to R.pptxIntroduction to R.pptx
Introduction to R.pptx
RohithK65
 
The Real Numbers
The Real NumbersThe Real Numbers
The Real Numbers
Sierra Nichols
 

Similar to R Programming (20)

Spreadsheets for developers
Spreadsheets for developersSpreadsheets for developers
Spreadsheets for developers
 
c++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptxc++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptx
 
SessionFour_DataTypesandObjects
SessionFour_DataTypesandObjectsSessionFour_DataTypesandObjects
SessionFour_DataTypesandObjects
 
Presentation of Linear Regression with Multiple Variable-4.pdf
Presentation of Linear Regression with Multiple Variable-4.pdfPresentation of Linear Regression with Multiple Variable-4.pdf
Presentation of Linear Regression with Multiple Variable-4.pdf
 
Array
ArrayArray
Array
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in R
 
1 1 number theory
1 1 number theory1 1 number theory
1 1 number theory
 
Numerical data.
Numerical data.Numerical data.
Numerical data.
 
An applied approach to calculas
An applied approach to calculasAn applied approach to calculas
An applied approach to calculas
 
Matrix decomposition and_applications_to_nlp
Matrix decomposition and_applications_to_nlpMatrix decomposition and_applications_to_nlp
Matrix decomposition and_applications_to_nlp
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming
 
Learning R while exploring statistics
Learning R while exploring statisticsLearning R while exploring statistics
Learning R while exploring statistics
 
Unit 3
Unit 3Unit 3
Unit 3
 
Ggplot2 work
Ggplot2 workGgplot2 work
Ggplot2 work
 
Introduction to R.pptx
Introduction to R.pptxIntroduction to R.pptx
Introduction to R.pptx
 
The Real Numbers
The Real NumbersThe Real Numbers
The Real Numbers
 

More from AKSHANSH MISHRA

Software packages for foundry use
Software packages for foundry useSoftware packages for foundry use
Software packages for foundry use
AKSHANSH MISHRA
 
FRICTION STIR WELDING PROJECT
FRICTION STIR WELDING PROJECTFRICTION STIR WELDING PROJECT
FRICTION STIR WELDING PROJECT
AKSHANSH MISHRA
 
5 hobbies that actually makes you smarter
5 hobbies that actually makes you smarter5 hobbies that actually makes you smarter
5 hobbies that actually makes you smarter
AKSHANSH MISHRA
 
fpc project
fpc projectfpc project
fpc project
AKSHANSH MISHRA
 
Portable Solar Laser Showers_Full Paper
Portable Solar Laser Showers_Full PaperPortable Solar Laser Showers_Full Paper
Portable Solar Laser Showers_Full Paper
AKSHANSH MISHRA
 
TAR ppt
TAR pptTAR ppt
INDUSTRIAL TRAINING
INDUSTRIAL TRAININGINDUSTRIAL TRAINING
INDUSTRIAL TRAINING
AKSHANSH MISHRA
 
nalco1122te
nalco1122tenalco1122te
nalco1122te
AKSHANSH MISHRA
 

More from AKSHANSH MISHRA (8)

Software packages for foundry use
Software packages for foundry useSoftware packages for foundry use
Software packages for foundry use
 
FRICTION STIR WELDING PROJECT
FRICTION STIR WELDING PROJECTFRICTION STIR WELDING PROJECT
FRICTION STIR WELDING PROJECT
 
5 hobbies that actually makes you smarter
5 hobbies that actually makes you smarter5 hobbies that actually makes you smarter
5 hobbies that actually makes you smarter
 
fpc project
fpc projectfpc project
fpc project
 
Portable Solar Laser Showers_Full Paper
Portable Solar Laser Showers_Full PaperPortable Solar Laser Showers_Full Paper
Portable Solar Laser Showers_Full Paper
 
TAR ppt
TAR pptTAR ppt
TAR ppt
 
INDUSTRIAL TRAINING
INDUSTRIAL TRAININGINDUSTRIAL TRAINING
INDUSTRIAL TRAINING
 
nalco1122te
nalco1122tenalco1122te
nalco1122te
 

Recently uploaded

Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdfAsymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
felixwold
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
Kamal Acharya
 
Butterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdfButterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdf
Lubi Valves
 
Accident detection system project report.pdf
Accident detection system project report.pdfAccident detection system project report.pdf
Accident detection system project report.pdf
Kamal Acharya
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
q30122000
 
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUESAN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
drshikhapandey2022
 
Sri Guru Hargobind Ji - Bandi Chor Guru.pdf
Sri Guru Hargobind Ji - Bandi Chor Guru.pdfSri Guru Hargobind Ji - Bandi Chor Guru.pdf
Sri Guru Hargobind Ji - Bandi Chor Guru.pdf
Balvir Singh
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
MadhavJungKarki
 
Ericsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.pptEricsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.ppt
wafawafa52
 
Presentation on Food Delivery Systems
Presentation on Food Delivery SystemsPresentation on Food Delivery Systems
Presentation on Food Delivery Systems
Abdullah Al Noman
 
Levelised Cost of Hydrogen (LCOH) Calculator Manual
Levelised Cost of Hydrogen  (LCOH) Calculator ManualLevelised Cost of Hydrogen  (LCOH) Calculator Manual
Levelised Cost of Hydrogen (LCOH) Calculator Manual
Massimo Talia
 
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
DharmaBanothu
 
Impartiality as per ISO /IEC 17025:2017 Standard
Impartiality as per ISO /IEC 17025:2017 StandardImpartiality as per ISO /IEC 17025:2017 Standard
Impartiality as per ISO /IEC 17025:2017 Standard
MuhammadJazib15
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
aryanpankaj78
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
SENTIMENT ANALYSIS ON PPT AND Project template_.pptx
SENTIMENT ANALYSIS ON PPT AND Project template_.pptxSENTIMENT ANALYSIS ON PPT AND Project template_.pptx
SENTIMENT ANALYSIS ON PPT AND Project template_.pptx
b0754201
 
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
sydezfe
 
FULL STACK PROGRAMMING - Both Front End and Back End
FULL STACK PROGRAMMING - Both Front End and Back EndFULL STACK PROGRAMMING - Both Front End and Back End
FULL STACK PROGRAMMING - Both Front End and Back End
PreethaV16
 
Beckhoff Programmable Logic Control Overview Presentation
Beckhoff Programmable Logic Control Overview PresentationBeckhoff Programmable Logic Control Overview Presentation
Beckhoff Programmable Logic Control Overview Presentation
VanTuDuong1
 
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
OKORIE1
 

Recently uploaded (20)

Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdfAsymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
 
Butterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdfButterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdf
 
Accident detection system project report.pdf
Accident detection system project report.pdfAccident detection system project report.pdf
Accident detection system project report.pdf
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
 
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUESAN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
 
Sri Guru Hargobind Ji - Bandi Chor Guru.pdf
Sri Guru Hargobind Ji - Bandi Chor Guru.pdfSri Guru Hargobind Ji - Bandi Chor Guru.pdf
Sri Guru Hargobind Ji - Bandi Chor Guru.pdf
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
 
Ericsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.pptEricsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.ppt
 
Presentation on Food Delivery Systems
Presentation on Food Delivery SystemsPresentation on Food Delivery Systems
Presentation on Food Delivery Systems
 
Levelised Cost of Hydrogen (LCOH) Calculator Manual
Levelised Cost of Hydrogen  (LCOH) Calculator ManualLevelised Cost of Hydrogen  (LCOH) Calculator Manual
Levelised Cost of Hydrogen (LCOH) Calculator Manual
 
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
 
Impartiality as per ISO /IEC 17025:2017 Standard
Impartiality as per ISO /IEC 17025:2017 StandardImpartiality as per ISO /IEC 17025:2017 Standard
Impartiality as per ISO /IEC 17025:2017 Standard
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
SENTIMENT ANALYSIS ON PPT AND Project template_.pptx
SENTIMENT ANALYSIS ON PPT AND Project template_.pptxSENTIMENT ANALYSIS ON PPT AND Project template_.pptx
SENTIMENT ANALYSIS ON PPT AND Project template_.pptx
 
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
 
FULL STACK PROGRAMMING - Both Front End and Back End
FULL STACK PROGRAMMING - Both Front End and Back EndFULL STACK PROGRAMMING - Both Front End and Back End
FULL STACK PROGRAMMING - Both Front End and Back End
 
Beckhoff Programmable Logic Control Overview Presentation
Beckhoff Programmable Logic Control Overview PresentationBeckhoff Programmable Logic Control Overview Presentation
Beckhoff Programmable Logic Control Overview Presentation
 
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
 

R Programming

  • 1. 1 R: THE TRUE BASICS R, also called the Languagefor Statistical Computing, was developed by Ross Ihaka and Robert Gentleman at the University of Auckland in the nineties. It is considered an open sourceimplementation of the S language, which was developed by John Chambers in the Bell Laboratories in the eighties. R provides a wide variety of statistical techniques and visualization capabilities. Another very importantfeature about R is that it is highly extensible. Because of this and more importantly becauseR is open source, it actually was the vehicle to bring the power of S to a larger community. Like in every programming language, there are pros and cons. ADVANTAGES: 1) It is an open sourceand free. 2) Master at graphics 3) Command – line Interface 4) Reproducibility through R scripts 5) R packages: Extensions of R DISADVANTAGES: 1) Easy to learn, harder to master 2) Poorly written code hard to read/maintain 3) Command – Line interface daunting at first 4) Poorly written code is slow The first step in R is one of the mostimportant components of R, and where most of the action happens, is the R console. It's a place where you can execute R commands. You simply type something at the promptin the console, hit Enter, and R interprets and executes your command.
  • 2. 2 Let's start our experiments by having R do some basic arithmetic; we'll calculate the sum of 1 and 2. We simply type 1 + 2 in the consoleand hit Enter. R compiles what you typed, calculates the result and prints that resultas a numerical value. Now let's try to type sometext in the console. We usedouble quotes for this. You can also simply type a number and hit Enter. R understood your character string and numerical value, but simply printed that string as an output. This sbrings meto the first super importantconcept in R: the variable. A variable allows you to storea value or an object in R. You can then later use this variable's name to easily access the value or the object that is stored within this variable. You can use the less than sign followed by a dash to create a variable.
  • 3. 3 Supposethe number 2 is the height of a rectangle. Let's assign this value 2 to a variable height. We type height, less than sign, dash, 2: This time, R does not print anything, because it assumes that you will be using this variable in the future. If we now simply type and execute height in the console, R returns 2: We can do a similar thing for the width of our imaginary rectangle. We assign the value 4 to a variablewidth. If wetype width, we see that indeed, it contains the value 4. As you'reassigning variables in the R console, you're actually accumulating an R workspace. It's theplace where variables and information is stored in R. You can access the objects in the workspacewith the ls() function. Simply type ls followed by empty parentheses and hit enter. This shows you a list of all the variables you have created in the R session. If you havefollowed all the examples up to now, you should see "height" and "width". This tells you that there are two objects in your workspaceatthe moment. When you type height in the console, R looks for the variable height in the workspace, finds it, and prints the corresponding value. If, however, wetry to printa non-existing variable, depth for example, R throws an error, becausedepth is not defined in the workspaceand thus not found. The principle of accumulating a workspace through variable assignmentmakes these variables available for further use. Supposewe wantto find out the area of our imaginary rectangle, which is height multiplied by width. Let's go ahead and type height asterisk width. The result is 8, as you'd expect. We can take it one step further and also assign the result of this calculation to a new variable, area. We again use the assignment operator. If you now type area, you'll see that it contains 8 as well. Inspecting
  • 4. 4 the workspaceagain with ls, shows thatthe workspacecontains threeobjects now: area, height and width.
  • 5. 5 Basic Data Types R's fundamental data types, also called atomic vector types. Throughoutour experiments, we will use the function class(). This is a usefulway to see what type a variable is. Let's head over to the consoleand start with TRUE, in capital letters. TRUE is a logical. That's also what class(TRUE) tells us. Logical are so- called boolean values, and can be either `TRUE` or `FALSE`. Well, actually, `NA`, to denote missing values, is also a logical. We can performall sorts of operations on them such as addition, subtraction, multiplication, division and many more. A special type of numeric is the integer. Itis a way to represent natural numbers like 1 and 2. To specify that a number is integer, you can add a capital L to them. We don't see the difference between the integer 2 and the numeric 2 from the output. However, the `class()` function reveals the difference. Instead of asking for the class of a variable, you can also use the is-dot-functions to see whether variables are actually of a certain type. To see if a variableis a numeric, we can usethe is- dot-numeric function. Itappears that both are numeric. To see if a variableis integer, we can use is-dot-integer. This shows us that integers are numeric, but that not all numeric are integers, so there's some
  • 6. 6 kind of type hierarchy going on here. Lastbut not least, there's the character string. The class of this type of object is "character". It's importantto note that there are other data types in R, such as double for higher precision numeric, complex for handling complex numbers, and raw to storeraw bytes.
  • 7. 7 Vectors 1) Create and name vectors: A vector is nothing more than a sequence of data elements of the _same_ basic data type. Firstthings first: creating a vector in R! You use the `c()` function for this, which allows us to combine values into a vector. Supposeyou'replaying a basic card game, and record the suit of 5 cards you draw from a deck. A possibleoutcome and corresponding vector to contain this information could be this one of coursewe could also assign this character vector to a new variable, drawn suits for example. We now have a character vector, drawn suits. Wecan assertthat it is a vector, by typing is dot vector drawn suits Likewise, we could create a vector of integers for example to storehow much cards of each suit remain after we drew the 5 cards. Let's call this vector remain. There are 11 more spades, 12 morehearts, 11 diamonds, and all 13 clubs still remain. .
  • 8. 8 We can use the `names ()` function for this. Let's firstcreate another character vector, `suits`, thatcontains the strings "spades", "hearts", "diamonds", and "clubs", the names we wantto give your vector elements. 2) Vector Arithmetic: We learned that we can usevariables to perform arithmetic Remember how you summed apples and oranges? From the previous section, we also know that actually these variables, `my_apples` and `my_oranges`,aresimply vectors. This means that we can perform arithmetic with vectors in R.
  • 9. 9 The most important thing to remember about operations with vectors in R , is that they will be applied element by element. This means that standard mathematics is extended to vectors in an element-wise fashion. Imagineyou have a vector containing your gambling earnings for the past 3 days. Not bad for a few days in the desert, is it? Imaginea well-dressed gentleman approaches you and offers to triple your earnings for the past three days, if you beat him in one round of poker. If you wantto calculate the expected earnings for each of the pastthree days, you can easily do it in R. As you can see, R multiplies each element in the `earnings` vector with 3, resulting in 150 dollars of promised earnings in the first day, 300 in the second day and 90 in the third day.. Likewise, division, subtraction, summation and many more are all carried out element wise, just as if you are carrying outthe operation between two scalars three times. Fromthese lines of code you don't see anything differentfrom what we'vedone before, becauseof course, you were working with vectors all along. The mathematics naturally extended to vectors that contain more than one element. Let's go back to your Vegas adventures. To enjoy your earnings, you also decided to go shopping and spend some money every day on the Las Vegas Strip. You recorded a vector of expenses. Because you are a very conscious programmer in training, you decide to compute whether your luck in the casino was sufficient to pay for your expenses.
  • 10. 10 MATRICES Creating and naming matrices: A matrix is kind of like the big brother of the vector. Where a vector is a sequence of data elements, which is one-dimensional, a matrix is a similar collection of data elements, but this time arranged into a fixed number of rows and columns. Since we are only working with rows and columns, a matrix is called two-dimensional. The matrix can contain only one atomic vector type. This means that you can't have logical and numeric in a matrix for example. There's really not much more theory about matrices than this: it's really a natural extension of the vector, going fromone to two dimensions. Of course, this has its implications for manipulating and subsetting matrices, but let's start with simply creating and naming them. To build a matrix, you usethe matrix function. Most importantly, it needs a vector, containing the values you want to place in the matrix, and at least one matrix dimension. You can chooseto specify the number of rows or the number of columns. Havea look at the following example, that creates a 2-by-3 matrixcontaining the values 1 to 6, by specifying the vector and setting the row argument to 2: R sees that the input vector has length 6 and that there haveto be two rows. Itthen infers that you'll probably want3 columns, such that the number of matrix elements matches the number of input vector elements.
  • 11. 11 If you prefer to fill up the matrix in a row-wisefashion, such that the 1, 2 and 3 are in the first row, you can set the `by row` argumentof matrix to `TRUE` Can you spot the difference? Remember how R did recycling when you weresubsetting vectors using logical vectors? The same thing happens when you pass the matrix function a vector that is too shortto fill up the entire matrix. Supposeyou pass a vector containing the values 1 to 3 to the matrix function, and explicitly say you wanta matrix with 2 rows and 3 columns: R fills up the matrix column by column and simply repeats the vector. If you try to fill up the matrix with a vector whosemultiple does not nicely fit in the matrix, for example when you want to put a 4-element vector in a 6- element matrix, R generates a warning message. Actually, apartfrom the `matrix()` function, there's yet another easy way to create matrices that is more intuitive in some cases. You can pastevectors together using the `cbind()` and `rbind()` functions. Havea look at these calls `cbind()`, shortfor column bind, takes the vectors you pass it, and sticks them together as if they were columns of a matrix. The `rbind()` function, shortfor row bind, does the samething but takes the input as rows and makes a matrix out of them. These functions can come in pretty handy, because they're often more easy to use than the `matrix()` function.
  • 12. 12 If you want to add another row to it, containing the values 7, 8, 9, you could simply run this command: You can do a similar thing with `cbind()`: Next up is naming the matrix. In the case of vectors, you simply used the names() function, but in the case of matrices, you could assign names to both columns and rows. That's why R came up with the rownames () and colnames () functions. Their use is pretty straightforward. Retaking thematrix `m` from before, we can set the row names justthe same way as wenamed vectors, but this time with the row names function.