Difference between revisions of "R/C2/Introduction-to-basics-of-R/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''FOSS:''' R through RStudio           R version 3.0.0 (2013-04-03) and RStudio version 0.97.336 '''Tutorial Title:''' Introduction to basics of R ''…')
 
 
(2 intermediate revisions by the same user not shown)
Line 12: Line 12:
  
 
'''Keywords: ''' Video tutorial, spoken tutorial, introduction to R, introduction to RStudio, mathematical operations, creation of vectors, sine of a vector, plotting with R
 
'''Keywords: ''' Video tutorial, spoken tutorial, introduction to R, introduction to RStudio, mathematical operations, creation of vectors, sine of a vector, plotting with R
 
  
 
{|Border=1
 
{|Border=1
Line 23: Line 22:
 
Opening slide
 
Opening slide
 
|Welcome to the spoken tutorial on basics of '''R'''.
 
|Welcome to the spoken tutorial on basics of '''R'''.
 
  
 
I am Kannan Moudgalya.
 
I am Kannan Moudgalya.
Line 31: Line 29:
  
 
Learning Objectives
 
Learning Objectives
 
 
 
 
 
 
 
  
 
| In this tutorial, we hope to provide a feel for '''R''' and '''RStudio'''
 
| In this tutorial, we hope to provide a feel for '''R''' and '''RStudio'''
Line 52: Line 43:
  
 
Prerequisites
 
Prerequisites
 
 
 
 
 
 
 
 
 
|  
 
|  
 
* To understand this tutorial, one needs to know elementary maths.
 
* To understand this tutorial, one needs to know elementary maths.
Line 71: Line 54:
  
 
Systems Requirements
 
Systems Requirements
 
 
 
  
 
| * I am using version '''3''' of '''R'''
 
| * I am using version '''3''' of '''R'''
Line 93: Line 73:
  
 
A Quick Introduction to R
 
A Quick Introduction to R
 
 
 
 
 
 
  
 
| Let us give a quick introduction to '''R''':
 
| Let us give a quick introduction to '''R''':
Line 111: Line 85:
  
 
A Quick Introduction to RStudio
 
A Quick Introduction to RStudio
 
  
 
| Let us now give a quick introduction to '''RStudio'''
 
| Let us now give a quick introduction to '''RStudio'''
Line 139: Line 112:
 
Practice session on R, using RStudio
 
Practice session on R, using RStudio
 
| Let us do a practice session on '''R''', using '''RStudio'''
 
| Let us do a practice session on '''R''', using '''RStudio'''
 
 
  
 
|-
 
|-
 
| Show RStudio
 
| Show RStudio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
 
| You now see '''RStudio''' on the screen.
 
| You now see '''RStudio''' on the screen.
 
  
 
I have shrunk its size quite a bit, to fit into a small screen.
 
I have shrunk its size quite a bit, to fit into a small screen.
 
  
 
Please use a size you are comfortable with.
 
Please use a size you are comfortable with.
 
  
 
I have also enlarged the font size, so that the recording is clear.
 
I have also enlarged the font size, so that the recording is clear.
 
  
 
You do this on '''Windows''' and '''Linux''', by pressing control and + keys together.
 
You do this on '''Windows''' and '''Linux''', by pressing control and + keys together.
 
  
 
For this tutorial, I want to maximise the size of the '''console''' of '''RStudio'''.
 
For this tutorial, I want to maximise the size of the '''console''' of '''RStudio'''.
 
  
 
I do this by pressing the '''maximise''' button in the '''console''' window.
 
I do this by pressing the '''maximise''' button in the '''console''' window.
 
  
 
I also maximise the right lower window of '''RStudio''' in a similar manner.
 
I also maximise the right lower window of '''RStudio''' in a similar manner.
Line 194: Line 134:
 
|-
 
|-
 
|2.1 * 5
 
|2.1 * 5
 
 
 
  
 
|Let me begin multiplying two numbers two point one and five.
 
|Let me begin multiplying two numbers two point one and five.
 
  
 
'''R''' gives the answer in black as ten point five.
 
'''R''' gives the answer in black as ten point five.
 
|-
 
|-
 
|2.1 * 5 /3
 
|2.1 * 5 /3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
 
|I want to divide this by three.
 
|I want to divide this by three.
 
  
 
I will recall the previous command by clicking the '''up arrow'''.
 
I will recall the previous command by clicking the '''up arrow'''.
 
  
 
What I typed earlier, namely two point one '''into''' five appears.
 
What I typed earlier, namely two point one '''into''' five appears.
 
  
 
I will divide this by three to get the answer three point five.
 
I will divide this by three to get the answer three point five.
Line 236: Line 155:
 
|-
 
|-
 
|<nowiki>a <- 2 + 3</nowiki>
 
|<nowiki>a <- 2 + 3</nowiki>
 
 
  
 
|I can store the result in a '''variable''' using the assignment operator:
 
|I can store the result in a '''variable''' using the assignment operator:
Line 249: Line 166:
 
|-
 
|-
 
|a
 
|a
 
 
 
  
 
|If I want to see what is in a '''variable''', I just type the '''variable name'''.
 
|If I want to see what is in a '''variable''', I just type the '''variable name'''.
 
  
 
In this case, I type '''a''' and see that it has five.
 
In this case, I type '''a''' and see that it has five.
Line 264: Line 177:
 
|-
 
|-
 
|(a - 0.16) ^ 0.5
 
|(a - 0.16) ^ 0.5
 
 
 
 
 
 
 
 
 
 
 
  
 
|I will raise it to the power of zero point 5.
 
|I will raise it to the power of zero point 5.
 
  
 
Remember, I have to put a minus zero point one six within '''brackets''', to get the correct answer.
 
Remember, I have to put a minus zero point one six within '''brackets''', to get the correct answer.
 
  
 
We get two point two, as expected.
 
We get two point two, as expected.
Line 294: Line 194:
 
|-
 
|-
 
|exp(1)
 
|exp(1)
 
 
 
 
 
 
 
 
 
  
 
|I will find the value of '''e''' by using the '''function call, exp'''.
 
|I will find the value of '''e''' by using the '''function call, exp'''.
 
  
 
You may have noticed, the moment I open a '''bracket, R''' automatically closes it.
 
You may have noticed, the moment I open a '''bracket, R''' automatically closes it.
 
  
 
This eliminates errors due to unmatched '''brackets'''.
 
This eliminates errors due to unmatched '''brackets'''.
 
  
 
So, we see the value of '''e''' above.
 
So, we see the value of '''e''' above.
Line 317: Line 205:
 
|-
 
|-
 
|log ( exp(1))
 
|log ( exp(1))
 
 
 
 
  
 
|I can find the natural '''logarithm''' of '''e''' with the help of the '''log function.'''
 
|I can find the natural '''logarithm''' of '''e''' with the help of the '''log function.'''
 
  
 
The value is one, as expected.
 
The value is one, as expected.
Line 329: Line 212:
 
|-
 
|-
 
|log10 ( 10^5 )
 
|log10 ( 10^5 )
 
 
 
 
 
 
 
 
 
  
 
|How do we find the '''log to the base ten'''?
 
|How do we find the '''log to the base ten'''?
 
  
 
Use the '''command log10.'''
 
Use the '''command log10.'''
 
  
 
Ten to the power five.
 
Ten to the power five.
 
  
 
This gives the answer as five, as expected.
 
This gives the answer as five, as expected.
Line 352: Line 223:
 
|-
 
|-
 
|log ( 10^5 )
 
|log ( 10^5 )
 
 
 
 
 
 
 
 
 
  
 
|What happens if I forget the ten in '''log10'''?
 
|What happens if I forget the ten in '''log10'''?
 
  
 
We get a different answer.
 
We get a different answer.
 
  
 
It is easy to verify that this answer is correct for natural '''logarithm'''.
 
It is easy to verify that this answer is correct for natural '''logarithm'''.
 
  
 
Can you please suggest a method to verify this?
 
Can you please suggest a method to verify this?
Line 375: Line 234:
 
|-
 
|-
 
|log (10^5,10)
 
|log (10^5,10)
 
 
 
 
 
 
  
  
 
|There is another way to find the '''logarithm to the base of 10'''.
 
|There is another way to find the '''logarithm to the base of 10'''.
 
  
 
Give the '''base''' as the second '''parameter''', as I do here.
 
Give the '''base''' as the second '''parameter''', as I do here.
 
  
 
We get five, as expected.
 
We get five, as expected.
Line 397: Line 248:
 
|-
 
|-
 
|z = seq (-1, 2, 0.5)
 
|z = seq (-1, 2, 0.5)
 
 
 
 
 
 
  
  
 
|'''z = s e q -1 comma 2 comma 0.5'''
 
|'''z = s e q -1 comma 2 comma 0.5'''
 
  
 
This creates a '''vector''' starting at minus one, going up to two, in increments of point five
 
This creates a '''vector''' starting at minus one, going up to two, in increments of point five
 
  
 
'''s e q''' denotes '''sequence'''.
 
'''s e q''' denotes '''sequence'''.
Line 423: Line 266:
 
|-
 
|-
 
|length (z)
 
|length (z)
 
 
 
  
 
|We can calculate the length of the '''vector z''' using the '''command length.'''
 
|We can calculate the length of the '''vector z''' using the '''command length.'''
 
  
 
We get the result seven, as expected.
 
We get the result seven, as expected.
Line 437: Line 276:
 
|-
 
|-
 
|pi
 
|pi
 
  
  
Line 444: Line 282:
 
|-
 
|-
 
|x = seq (-2*pi, 2*pi, 1)
 
|x = seq (-2*pi, 2*pi, 1)
 
 
 
 
 
  
 
|Let us create a '''vector x''' to start from minus two pi, go up to plus two pi, in increments of one.
 
|Let us create a '''vector x''' to start from minus two pi, go up to plus two pi, in increments of one.
 
  
 
Please remember to put the multiply sign.
 
Please remember to put the multiply sign.
Line 461: Line 293:
 
|-
 
|-
 
|y = sin(x)
 
|y = sin(x)
 
 
 
  
 
|Let us find the '''sine''' of this '''vector''' using '''sin''' and store it in '''y'''.
 
|Let us find the '''sine''' of this '''vector''' using '''sin''' and store it in '''y'''.
 
  
 
We can work directly with '''vectors''' in '''R''', as we did above.
 
We can work directly with '''vectors''' in '''R''', as we did above.
Line 476: Line 304:
 
|-
 
|-
 
|plot (x, y, type=”l”)
 
|plot (x, y, type=”l”)
 
 
 
 
 
 
 
 
 
 
  
  
 
|Let us plot '''sin(x) versus x''', with '''type=L argument''' added.
 
|Let us plot '''sin(x) versus x''', with '''type=L argument''' added.
 
  
 
Note that '''x''' comes first and then '''y'''.
 
Note that '''x''' comes first and then '''y'''.
 
  
 
This plot starts from zero as expected, because '''sin''' of minus two pi is zero.
 
This plot starts from zero as expected, because '''sin''' of minus two pi is zero.
 
  
 
The plot is not smooth, as there are not many points.
 
The plot is not smooth, as there are not many points.
Line 505: Line 320:
 
|-
 
|-
 
|plot(x,y,type=”l”)
 
|plot(x,y,type=”l”)
 
 
 
 
 
 
  
  
 
|What happens if I try to plot without recalculating '''y'''?
 
|What happens if I try to plot without recalculating '''y'''?
 
  
 
'''R''' will complain that the lengths are unequal.
 
'''R''' will complain that the lengths are unequal.
 
  
 
You should pause the video and confirm that the lengths are unequal.
 
You should pause the video and confirm that the lengths are unequal.
Line 527: Line 334:
 
|-
 
|-
 
|plot(x, y, type=”l”)
 
|plot(x, y, type=”l”)
 
 
 
 
 
 
 
 
 
 
 
  
 
|Let me re-plot this curve.
 
|Let me re-plot this curve.
 
  
 
You can see that the plot is smoother now.
 
You can see that the plot is smoother now.
 
  
 
Please pause the video now and look at the values stored in '''x''' and '''y'''.
 
Please pause the video now and look at the values stored in '''x''' and '''y'''.
 
  
 
Please confirm that these values are as per expectation.
 
Please confirm that these values are as per expectation.
Line 554: Line 347:
  
 
length(y)
 
length(y)
 
 
 
 
 
  
 
|We see the lengths of '''x''' and '''y''' to be 126.
 
|We see the lengths of '''x''' and '''y''' to be 126.
 
  
 
You should verify that these lengths are correct.
 
You should verify that these lengths are correct.
 
  
 
You should also display the values of x and y and verify.
 
You should also display the values of x and y and verify.
Line 575: Line 361:
 
|-
 
|-
 
|Press help
 
|Press help
 
 
 
 
 
 
 
 
 
 
  
 
|Let us press the '''help button''' in the right hand side window.
 
|Let us press the '''help button''' in the right hand side window.
 
  
 
Let us type '''plot''' next to the '''lens''' symbol and hit return.
 
Let us type '''plot''' next to the '''lens''' symbol and hit return.
 
  
 
Let us scroll.
 
Let us scroll.
 
  
 
One can see different choices for the '''type''' parameter.
 
One can see different choices for the '''type''' parameter.
Line 611: Line 384:
  
 
'''Summary'''
 
'''Summary'''
 
 
 
 
 
  
  
Line 628: Line 396:
  
 
'''Assignment'''
 
'''Assignment'''
 
 
 
 
 
 
 
 
 
 
  
 
|We now suggest an assignment
 
|We now suggest an assignment
Line 654: Line 412:
  
 
|This video summarises the Spoken Tutorial project.
 
|This video summarises the Spoken Tutorial project.
 
  
 
If you do not have good bandwidth, you may download and watch it.
 
If you do not have good bandwidth, you may download and watch it.
Line 662: Line 419:
  
 
Spoken Tutorial Workshops
 
Spoken Tutorial Workshops
 
 
 
  
 
|We conduct workshops using Spoken Tutorials.
 
|We conduct workshops using Spoken Tutorials.
 
  
 
Give Certificates.
 
Give Certificates.
 
  
 
Please contact us.
 
Please contact us.

Latest revision as of 23:46, 4 October 2019

FOSS: R through RStudio

          R version 3.0.0 (2013-04-03) and RStudio version 0.97.336

Tutorial Title: Introduction to basics of R

Author: Kannan Moudgalya

Reviewer: Neeraj Hatekar, S. Subramanian, T. Santhanam, Sanjeev Bakshi, Revathi Kasturi and others

Date: 25 August 2013

Keywords: Video tutorial, spoken tutorial, introduction to R, introduction to RStudio, mathematical operations, creation of vectors, sine of a vector, plotting with R

Visual Cue Narration
Slide 1

Opening slide

Welcome to the spoken tutorial on basics of R.

I am Kannan Moudgalya.

Slide 2

Learning Objectives

In this tutorial, we hope to provide a feel for R and RStudio
  • How can R be used as a calculator?
  • How to use built-in functions like sin and log?
  • Introduction to vector operations
  • Introduction to plotting
  • Taking help in RStudio and
  • Where to get more information on R and RStudio
Slide 3

Prerequisites

  • To understand this tutorial, one needs to know elementary maths.
    • For example, log, sine and plotting
  • Please locate this tutorial on our website, spoken tutorial dot org
  • We provide many useful supplementary material in this page
  • No programming background is required, however, to understand this tutorial
Slide 4

Systems Requirements

* I am using version 3 of R
  • Using RStudio 0.97
  • R and RStudio work on web browsers also
  • Hence, these are easily accessible from every operating system, including Aakash
Slide 5

System Requirements, ctnd.

Let us continue with system requirements.
  • For this tutorial, I am using Mac OS X 10.7.4.
  • But the usage is similar in Linux and Windows as well
Slide 6

A Quick Introduction to R

Let us give a quick introduction to R:
  • R is an outstanding software for general computations
  • It is especially suitable for statistical computing
  • It is an alternative to SPSS
  • It is a free and open source software
  • It can be downloaded from c ran dot r dash project dot org
Slide 7

A Quick Introduction to RStudio

Let us now give a quick introduction to RStudio
  • RStudio is a friendly front-end to R
  • It is a free and open source software
  • It can be downloaded from rstudio dot com
Show http://www.rstudio.com/ide A two minute video by the RStudio team is available here.
Slide 7 contd. Let me return to the slides.
  • RStudio also runs on a web browser.
Slide 8

R, RStudio web server from Aakash

Here is a screenshot of R and RStudio on a web server, accessed from Aakash
Slide 9

Practice session on R, using RStudio

Let us do a practice session on R, using RStudio
Show RStudio You now see RStudio on the screen.

I have shrunk its size quite a bit, to fit into a small screen.

Please use a size you are comfortable with.

I have also enlarged the font size, so that the recording is clear.

You do this on Windows and Linux, by pressing control and + keys together.

For this tutorial, I want to maximise the size of the console of RStudio.

I do this by pressing the maximise button in the console window.

I also maximise the right lower window of RStudio in a similar manner.

2.1 * 5 Let me begin multiplying two numbers two point one and five.

R gives the answer in black as ten point five.

2.1 * 5 /3 I want to divide this by three.

I will recall the previous command by clicking the up arrow.

What I typed earlier, namely two point one into five appears.

I will divide this by three to get the answer three point five.

2 + 3 I can add two numbers.
a <- 2 + 3 I can store the result in a variable using the assignment operator:

Less than symbol followed by hyphen.

a = 2 + 3 One can also use an equal sign.
a If I want to see what is in a variable, I just type the variable name.

In this case, I type a and see that it has five.

a - 0.16 I will subtract zero point one six  from a.
(a - 0.16) ^ 0.5 I will raise it to the power of zero point 5.

Remember, I have to put a minus zero point one six within brackets, to get the correct answer.

We get two point two, as expected.

Using the left and right arrows, I moved the cursor back and forth within a command.
sqrt ( a - 0.16 ) I can also carry out the same calculation using the function call, square root.
exp(1) I will find the value of e by using the function call, exp.

You may have noticed, the moment I open a bracket, R automatically closes it.

This eliminates errors due to unmatched brackets.

So, we see the value of e above.

log ( exp(1)) I can find the natural logarithm of e with the help of the log function.

The value is one, as expected.

log10 ( 10^5 ) How do we find the log to the base ten?

Use the command log10.

Ten to the power five.

This gives the answer as five, as expected.

log ( 10^5 ) What happens if I forget the ten in log10?

We get a different answer.

It is easy to verify that this answer is correct for natural logarithm.

Can you please suggest a method to verify this?

log (10^5,10)


There is another way to find the logarithm to the base of 10.

Give the base as the second parameter, as I do here.

We get five, as expected.

It is easy to create vectors in R.
z = seq (-1, 2, 0.5)


z = s e q -1 comma 2 comma 0.5

This creates a vector starting at minus one, going up to two, in increments of point five

s e q denotes sequence.

z Let us see the value of z.
We will see other ways to create vectors in another tutorial.
length (z) We can calculate the length of the vector z using the command length.

We get the result seven, as expected.

We will now discuss pi.
pi


The value of pi, is stored in the variable pi, p i, which we can see now.
x = seq (-2*pi, 2*pi, 1) Let us create a vector x to start from minus two pi, go up to plus two pi, in increments of one.

Please remember to put the multiply sign.

x Let us see what x contains
y = sin(x) Let us find the sine of this vector using sin and store it in y.

We can work directly with vectors in R, as we did above.

y Let us see what y contains.
plot (x, y, type=”l”)


Let us plot sin(x) versus x, with type=L argument added.

Note that x comes first and then y.

This plot starts from zero as expected, because sin of minus two pi is zero.

The plot is not smooth, as there are not many points.

x = seq(-2*pi, 2*pi, 0.1) Let us now get more points by lowering the increment number to point one.
plot(x,y,type=”l”)


What happens if I try to plot without recalculating y?

R will complain that the lengths are unequal.

You should pause the video and confirm that the lengths are unequal.

y = sin(x) Let me recalculate y.
plot(x, y, type=”l”) Let me re-plot this curve.

You can see that the plot is smoother now.

Please pause the video now and look at the values stored in x and y.

Please confirm that these values are as per expectation.

length(x)

length(y)

We see the lengths of x and y to be 126.

You should verify that these lengths are correct.

You should also display the values of x and y and verify.

plot(x, y) If we drop the type parameter in the previous command, we get a plot with points.
Press help Let us press the help button in the right hand side window.

Let us type plot next to the lens symbol and hit return.

Let us scroll.

One can see different choices for the type parameter.

We have come to the end of this tutorial.
Slide 10

Low cost books on R

Let us see a few low cost books on R.
Slide 11

Summary


In this tutorial we learnt about
  • A few elementary operations
  • Introduction to plotting
  • Introduction to help
  • URLs and books on R and RStudio
Slide 12

Assignment

We now suggest an assignment
  • Find answers to log2 (2^5) and log (e^2)
  • Please explain your answers
  • Explore the Help tab in RStudio: Change the labels, title, etc.
  • Hint: try the plot command with x l a b argument
  • Copy paste a few examples provided in Help and see what they do
Slide 13

About the Spoken Tutorial Project


This video summarises the Spoken Tutorial project.

If you do not have good bandwidth, you may download and watch it.

Slide 14

Spoken Tutorial Workshops

We conduct workshops using Spoken Tutorials.

Give Certificates.

Please contact us.

Slide 15

Acknowledgements

The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
Slide 16 Thanks Thanks for joining, goodbye

Contributors and Content Editors

Nancyvarkey, Sudhakarst