Posts

if and if-else Statements in R

Control statements are expressions used to control the execution and flow of the program based on the conditions provided in the statements.These statements are used to make a decision after assessing the variable.  if condition This control structure checks the expression provided in parenthesis is true or not. If true, the execution of the statements in braces {} continues. Syntax: if(expression)           { statements .... ....            } Example: x <- 100 if(x > 10){ print(paste(x, "is greater than 10")) } output [1] "100 is greater than 10" if-else condition It is similar to if condition but when the test expression in if condition fails, then statements in else condition are executed. Syntax:                if(expression)                                  ...

switch statement in R

Switch case statements are a substitute for long if statements that compare a variable to several integral values. Switch case in R is a multiway branch statement. It allows a variable to be tested for equality against a list of values. Switch statement follows the approach of mapping and searching over a list of values. If there is more than one match for a specific value, then the switch statement will return the first match found of the value matched with the expression. Syntax: switch(expression, case1, case2, case3....) Here, the expression is matched with the list of values and the corresponding value is returned. Important Points about Switch Case Statements: An expression type with character string always matched to the listed cases. An expression which is not a character string then this exp is coerced to integer. For multiple matches, the first match element will be used. No default argument case is available there in R switch case. An unnamed case can be used, if there is no...

for loop in R - break and next statement

For loop in R Programming Language is useful to iterate over the elements of a list, data frame, vector, matrix, or any other object. It means, the for loop can be used to execute a group of statements repeatedly depending upon the number of elements in the object. It is an entry controlled loop, in this loop the test condition is tested first, then the body of the loop is executed, the loop body would not be executed if the test condition is false. for loop It is a type of loop or sequence of statements executed repeatedly until exit condition is reached. Syntax: for(value in vector) {    statements .... ....  } Example: for (i in seq(10)){   cat(paste(i," ")) } output: 1  2  3  4  5  6  7  8  9  10   for (i in 1:10){   cat(paste(i," ")) } output: 1  2  3  4  5  6  7  8  9  10   x <- letters[4:10] for(i in x) { print(i) } output [1] "d" [1] "e" [1]...

while loop in R

While loop in R programming language is used when the exact number of iterations of a loop is not known beforehand. It executes the same code again and again until a stop condition is met. While loop checks for the condition to be true or false n+1 times rather than n times. This is because the while loop checks for the condition before entering the body of the loop. R- While loop Syntax:  while (test_expression) {   statement update_expression } How does a While loop execute?  Control falls into the while loop. The flow jumps to Condition Condition is tested. If the Condition yields true, the flow goes into the Body. If the Condition yields false, the flow goes outside the loop The statements inside the body of the loop get executed. Updation takes place. Control flows back to Step 2. The while loop has ended and the flow has gone outside. Important Points about while loop in R language: It seems to be that while the loop will run forever but it is not true, condit...

repeat loop in R

Repeat loop in R is used to iterate over a block of code multiple number of times. And also it executes the same code again and again until a break statement is found. Repeat loop, unlike other loops, doesn’t use a condition to exit the loop instead it looks for a break statement that executes if a condition within the loop body results to be true. An infinite loop in R can be created very easily with the help of the Repeat loop. The keyword used for the repeat loop is 'repeat'. Syntax : repeat {  commands   if(condition) {   break }  } # R program to illustrate repeat loop result <- 1 # test expression repeat { print(result) # update expression result = result + 1 # Breaking condition if(result > 5) { break } } output: [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 #checking for prime n <- 21 b <- a/2 flag <- 0 i <- 2 repeat { if ((n %% i)== 0) { flag <- 1 break } if (i>b) { break}     i=i+1 } if (flag == 1) { print("c...

Functions in R

Functions are useful when you want to perform a certain task multiple times. A function accepts input arguments and produces the output by executing valid R commands that are inside the function. In R Programming Language when you are creating a function the function name and the file in which you are creating the function need not be the same and you can have one or more functions in R. Creating a Function in R Functions are created in R by using the command function(). The general structure of the function file is as follows: Syntax: function_name = function(arg_1, arg_2, …) { Function body }   The various components/parts of a function are: Function name: It is the actual name of the function. It is stored in R environment as an object with this name. Arguments: An argument is a placeholder. Whenever a function is invoked, a value if passed to the argument.They are optional; that is, a function may contain no arguments. Also arguments can have default values. Function Body: I...

Recursive Functions in R

Recursion, in the simplest terms, is a type of looping technique. It exploits the basic working of functions in R. Recursion is when the function calls itself. This forms a loop, where every time the function is called, it calls itself again and again and this technique is known as recursion. Since the loops increase the memory we use the recursion. The recursive function uses the concept of recursion to perform iterative tasks they call themselves, again and again, which acts as a loop. These kinds of functions need a stopping condition so that they can stop looping continuously. Recursive functions call themselves. They break down the problem into smaller components. The function() calls itself within the original function() on each of the smaller components. After this, the results will be put together to solve the original problem. Example: Factorial using Recursion in R factr <- function(n){ if(n==0 || n==1) { return(1) }else { return(n*factr(n-1)) } } factr(5) ...