Tuesday, January 29, 2019

Control and Loop Statements in R

Control and Loop Statements in R.

There are 2 types of Control/Conditional statements and 3 types of Loop available in R. see below

See Also: Introduction to R/ Basic functions of R

Conditional Statements
1. if ... else
2. ifelse() Function

Loop Statements
1. for loop
2. while loop
3. repeat loop

Related Statements/Topics
1. break
2. next
3. logical expression

Conditional Statements
1. if...else Like other programming languages, the if...else statement is the same in R

NOTE: else if/ else keyword should be placed in the next of closing } of the earlier block.

if(logic_expression) {
stmt.
} else if (logic_expression) {
stmt.
} else {
stmt
}

Example 1
x <- 5
if(x>10) {
print("grater than 10")
}

Example 2
x <- 5
if(x>10) {
print("grater than 10")
} else {
print("less than 10")
}

Example 3
x <- 5
if(x>10) {
print("grater than 10")
} else if (x>5 & x<10) {
print("less than 10 BUT grater than 5")
} else {
print("less than 5")
}

2. ifelse() is a function to use with vector AND the return would be a vector also.

Example 1
x = 1:10
ifelse( x %% 2 == 0 , "even", "odd")
OUTPUT: [1] "odd"  "even" "odd"  "even" "odd"  "even" "odd"  "even" "odd"  "even"

Loop Statements
1. for loop: the basic is the same as other languages BUT the syntex is different

for ( val in vector/sequence )
{
    statement
}

Example 1
v = c(1,2,3,4,5)
for ( x in v )
{
    print (x)
}

Example 2: Fibonacci Series upto 10th number
f = c(1,1)
for (x in 1:8) {
f <- c(f, sum( tail(f,2) ) )
}
print(f)
OUTPUT: [1]   1   1   2   3   5   8  13  21  34  55  89 144

Example 3: Nested For Loop - a simple pyramid of numbers
for(x in 1:9){
p = NULL
for(y in 1:x){
p = c(p, y)
}
print(p)
}
OUTPUT:
[1] 1
[1] 1 2
[1] 1 2 3
[1] 1 2 3 4
[1] 1 2 3 4 5
[1] 1 2 3 4 5 6
[1] 1 2 3 4 5 6 7
[1] 1 2 3 4 5 6 7 8
[1] 1 2 3 4 5 6 7 8 9

2. while loop: basic is also same for this one.

while( logic_expression )
{
    statement
}

Example 1
x=1
y=10
while(x<y)
{
  print(x)
  x = x + 1
}

Example 2 : Create a vector with the sum of current value from the first value [ x=1⅀x=n ].
v=1:10
x=1
sumV = NULL
while ( x <= length(v) )
{
sumV = c(sumV , sum( head(v,x) ))
x = x + 1
}
print(v)
print(sumV)

OUTPUT:
v ==>        [1]  1  2  3  4  5  6  7  8  9 10
sumV ==> [1]  1  3  6 10 15 21 28 36 45 55

3. repeat loop: it just repeats the block until finding a break statement.

repeat {
statement
}

Example 1:
r = 0
count = 1
repeat {
r = c(r,count)
if(count == 5) { break }
count = count + 1
}
print(r)
OUTPUT: [1] 0 1 2 3 4 5

Related Statements/Topics
1. break: this is like other languages. just break a loop to continue and goto the end of the loop.
2. next: this is like continue of other languages. just continue the next iteration by skipping the following statements.
Example 1:
r=NULL
for (x in 1:20) {
if(x == 5) { next}
  if(x == 10) { break}
r = c(r,x)
count = count + 1
}
print(r)
OUTPUT:
In output 5 is missing and loop stopes at 10!
[1] 1 2 3 4 6 7 8 9

3. logical expression: this is also like other languages.
R supports the combination of logical operators..
x > 2 & y <5
x > 2 && y <5
x > 2 | y <5
x > 2 || y <5
for vector:
xV  & yV ==> will check all the elements and return a logical vector
xV  && yV ==> will check the first elements only and return a logical value
xV  || yV ==> will check the first elements only and return a logical value
all logical operators:
&, &&, | ,||, ! (NOT)

1 comment:

  1. Nothing Is Bug …: Control And Loop Statements In R >>>>> Download Now

    >>>>> Download Full

    Nothing Is Bug …: Control And Loop Statements In R >>>>> Download LINK

    >>>>> Download Now

    Nothing Is Bug …: Control And Loop Statements In R >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete