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 ''…')
 
Line 31: Line 31:
  
 
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 45:
  
 
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 56:
  
 
Systems Requirements
 
Systems Requirements
 
 
 
  
 
| * I am using version '''3''' of '''R'''
 
| * I am using version '''3''' of '''R'''
Line 93: Line 75:
  
 
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 139: Line 115:
 
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.
Line 194: Line 145:
 
|-
 
|-
 
|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.
Line 204: Line 152:
 
|-
 
|-
 
|2.1 * 5 /3
 
|2.1 * 5 /3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
 
|I want to divide this by three.
 
|I want to divide this by three.
Line 236: Line 170:
 
|-
 
|-
 
|<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 181:
 
|-
 
|-
 
|a
 
|a
 
 
  
  
Line 264: Line 194:
 
|-
 
|-
 
|(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.
Line 294: Line 213:
 
|-
 
|-
 
|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'''.
Line 317: Line 227:
 
|-
 
|-
 
|log ( exp(1))
 
|log ( exp(1))
 
 
 
  
  
Line 329: Line 236:
 
|-
 
|-
 
|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'''?
Line 352: Line 250:
 
|-
 
|-
 
|log ( 10^5 )
 
|log ( 10^5 )
 
 
 
 
 
 
 
 
 
  
 
|What happens if I forget the ten in '''log10'''?
 
|What happens if I forget the ten in '''log10'''?
Line 375: Line 264:
 
|-
 
|-
 
|log (10^5,10)
 
|log (10^5,10)
 
 
 
 
 
 
  
  
Line 397: Line 280:
 
|-
 
|-
 
|z = seq (-1, 2, 0.5)
 
|z = seq (-1, 2, 0.5)
 
 
 
 
 
 
  
  
Line 423: Line 300:
 
|-
 
|-
 
|length (z)
 
|length (z)
 
 
  
  
Line 437: Line 312:
 
|-
 
|-
 
|pi
 
|pi
 
  
  
Line 444: Line 318:
 
|-
 
|-
 
|x = seq (-2*pi, 2*pi, 1)
 
|x = seq (-2*pi, 2*pi, 1)
 
 
 
 
  
  
Line 461: Line 331:
 
|-
 
|-
 
|y = sin(x)
 
|y = sin(x)
 
 
  
  
Line 476: Line 344:
 
|-
 
|-
 
|plot (x, y, type=”l”)
 
|plot (x, y, type=”l”)
 
 
 
 
 
 
 
 
 
 
  
  
Line 505: Line 363:
 
|-
 
|-
 
|plot(x,y,type=”l”)
 
|plot(x,y,type=”l”)
 
 
 
 
 
 
  
  
Line 527: Line 379:
 
|-
 
|-
 
|plot(x, y, type=”l”)
 
|plot(x, y, type=”l”)
 
 
 
 
 
 
 
 
 
 
  
  
Line 554: Line 396:
  
 
length(y)
 
length(y)
 
 
 
 
  
  
Line 575: Line 413:
 
|-
 
|-
 
|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.
Line 611: Line 439:
  
 
'''Summary'''
 
'''Summary'''
 
 
 
 
 
  
  
Line 628: Line 451:
  
 
'''Assignment'''
 
'''Assignment'''
 
 
 
 
 
 
 
 
 
 
  
 
|We now suggest an assignment
 
|We now suggest an assignment
Line 662: Line 475:
  
 
Spoken Tutorial Workshops
 
Spoken Tutorial Workshops
 
 
 
  
 
|We conduct workshops using Spoken Tutorials.
 
|We conduct workshops using Spoken Tutorials.

Revision as of 16:28, 24 April 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