R/C2/Functions-in-R/English-timed
From Script | Spoken-Tutorial
Time | Narration |
00:00 | Welcome to this tutorial on Functions in R |
00:05 | In this tutorial, we will learn: Need for functions |
00:10 | How to create a user-defined function |
00:15 | Scope of variables |
00:17 | To understand this tutorial, you should know, |
00:21 | Basics of permutation and combination |
00:25 | Basic data structures |
00:28 | Conditional statements |
00:31 | If not, please locate the relevant tutorials on R on this website. |
00:38 | This tutorial is recorded on Ubuntu Linux OS version 16.04 |
00:47 | R version 3.4.4 |
00:51 | RStudio version 1.1.463 |
00:56 | Install R version 3.2.0 or higher. |
01:02 | For this tutorial, we will use |
01:05 | A script file myFunctions.R. |
01:10 | Please download this file from the Code files link of this tutorial. |
01:18 | I have downloaded and moved this file to functions folder. |
01:24 | This folder is located in myProject folder on my Desktop. |
01:31 | I have also set functions folder as my Working Directory. |
01:38 | A function is a set of statements organized together to perform a specific task. |
01:45 | R has a large number of built-in functions. |
01:49 | In spite of that, sometimes we need to define our own functions. |
01:56 | User-defined functions are specific to user’s requirements. |
02:02 | Once created these functions can be used as the built-in functions. |
02:08 | An R function is created by using the keyword function. |
02:14 | The syntax of an R function is as follows: |
02:20 | The different parts of a function are: Name, Arguments, Body, Return value. |
02:32 | Let us switch to RStudio. |
02:35 | Open the script myFunctions.R in RStudio. |
02:42 | Here, we have declared a sample vector named sampleVec. |
02:48 | Let us find the summary of this vector by using the built-in function. |
02:55 | In the Source window, type the following command. |
02:59 | Save the script and run this script by clicking on the Source button. |
03:07 | The summary of sampleVec is displayed. |
03:12 | It includes median, mean, etc. |
03:18 | Similarly, R has many built-in functions to make our life easy. |
03:24 | But we can always create a function of our own, depending on the need. |
03:30 | We will create a function that computes the permutation of two numbers. |
03:36 | In the Source window, type the following command. |
03:42 | Here, we have named our function as permutation. |
03:48 | Next, we have passed two values as arguments. |
03:53 | Inside the function, the definition of permutation has been written. |
03:58 | Run the current line by pressing Ctrl + Enter keys simultaneously. |
04:05 | Now, let us test this function to check whether it gives correct results. |
04:11 | First, we will calculate the permutation of 5 and 2. |
04:17 | In the Source window, type permutation and in parentheses 5 comma 2. |
04:27 | Run the current line. |
04:29 | The required value of 20 is displayed. |
04:34 | Thus, our function has given the correct result. |
04:39 | While creating functions, the arguments are optional. |
04:44 | Let us create a function that does not take any arguments. |
04:49 | We will create a function named as votingEligibility. |
04:54 | In the Source window, type the following commands. |
04:59 | This function should print a message whether an Indian citizen is eligible to vote or not. |
05:08 | Remember the minimum age for Indian citizens to vote is 18 years. |
05:15 | Here, we have not passed any arguments. |
05:20 | readline function reads a line from the terminal in interactive use. |
05:27 | Run this block of code to execute the function votingEligibility. |
05:33 | Now, we will run this function to see whether it works. |
05:38 | In the Source window, type the following command. |
05:42 | Please note that we are not passing any arguments inside this function. |
05:49 | Run the current line. |
05:52 | We are asked to enter our age. |
05:56 | Let us type 21 and press Enter. |
06:02 | We get a message, You can cast your vote. |
06:08 | Now let us say we want to create a function sum_between_two such that |
06:17 | it takes two natural numbers num1 and num2 as its arguments. |
06:26 | Then, it returns the sum of all numbers from num1 to num2. |
06:32 | For example, if we pass 2 and 6 to the function sum_between_two, |
06:42 | it should return the value of 2 + 3 + 4 + 5 + 6. |
06:50 | Let us switch back to RStudio. |
06:54 | In the Source window, type the following command. |
06:59 | We have named our function as sum_between_two. |
07:08 | Also, we have specified the two arguments num1 and num2. |
07:14 | Inside the function, we have initialized an object named result to store the sum. |
07:22 | We have created a for loop which will increment the result at each iteration. |
07:31 | Run this block of code to execute the function. |
07:36 | Now we test our function sum_between_two by running it with two arguments. |
07:45 | In the Source window, type the following command. |
07:51 | Run the current line. |
07:53 | The function with arguments 2 and 6 was executed, but we got nothing as output. |
08:03 | Let us investigate what went wrong. |
08:07 | We know that our function got executed as we did not get any error after executing this line. |
08:16 | It means that the object named result got incremented. |
08:22 | Now, we will check the value of the result. |
08:26 | In the Source window, type the following command. |
08:31 | Run the current line. |
08:34 | It shows, Error in print(result) : object 'result' not found. |
08:40 | To understand what went wrong, we need to learn a concept named as Scope. |
08:48 | Scope is the term used to describe how objects and variables get defined within R. |
08:56 | If a variable is defined inside a function, then it can be accessed inside the function only. |
09:04 | So, if we try to access the same variable outside the function, it will throw an error. |
09:12 | Let us switch to RStudio. |
09:17 | Here, the variable result is defined inside the function. |
09:22 | So its scope is limited to this function only. |
09:27 | Hence, when we tried to access this variable outside the function, it threw an error. |
09:34 | For this problem, we can use return function. |
09:39 | Let us get back to our function sum_between_two. |
09:47 | Press Enter at the end of for loop. |
09:53 | Now type the following command. |
09:56 | I will resize the Console window. |
10:01 | Select all lines of the function sum_between_two.. |
10:08 | Now press Ctrl + Enter keys together to run the updated function. |
10:17 | Let us run the function again with arguments 2 and 6. |
10:23 | For this, we will run this line. |
10:27 | We see the output 20 as expected. |
10:32 | Let us summarize what we have learnt. |
10:36 | In this tutorial, we have learnt: Need for functions |
10:41 | How to create a user-defined function |
10:45 | Scope of variables |
10:48 | We now suggest an assignment. |
10:51 | Create a function which computes combination of the two numbers. |
10:57 | Create a function which takes a natural number as an argument, and prints Fibonacci series |
11:05 | For example, consider fibonacci(5). It should print the first 5 elements of Fibonacci series, i.e. 1, 1, 2, 3, 5. |
11:19 | The video at the following link summarises the Spoken Tutorial project. |
11:24 | Please download and watch it. |
11:27 | We conduct workshops using Spoken Tutorials and give certificates. |
11:33 | Please contact us. |
11:36 | Please post your timed queries in this forum. |
11:41 | Please post your general queries in this forum. |
11:44 | The FOSSEE team coordinates the TBC project. |
11:48 | For more details, please visit these sites. |
11:52 | The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India |
11:59 | The script for this tutorial was contributed by Varshit Dubey (College of Engineering Pune). |
12:07 | This is Sudhakar Kumar from IIT Bombay signing off. |
12:12 | Thanks for watching. |