A Vectorized if-then-else : The ifelse() Statement

Like the if-then-else construct found in most languages, R also includes a vectorized version, the ifelse() function. This the short form of the traditional IF Else statement.

>ifelse(b,u,v)

where b is a Boolean vector, and u and v are vectors.

The return value is itself a vector; element i is u[i] if b[i] is true, or v[i] if b[i] is false.

Example:

> x <- 1:10

# %% is the mod operator

> y <- ifelse(x %% 2 == 0,5,12)

> y

[1] 12 5 12 5 12 5 12 5 12 5

Here, we wish to produce a vector in which there is a 5 wherever x is even or a 12 wherever x is odd. So, the actual argument corresponding to the formal argument b is (F,T,F,T,F,T,F,T, F,T). The second actual argument,5, corresponding to u, is treated as (5,5,...)(ten 5s) by recycling. The third argument, 12, is also recycled, to (12,12,...).

Here is another example:

> x <- c(5,2,9,12)

> ifelse(x > 6,2*x,3*x)

[1] 15 6 18 24

We return a vector consisting of the elements of x, either multiplied by 2 or 3, depending on whether the element is greater than 6. The expression x > 6 is a vector of Booleans. If the ith component is true, then the ith element of the return value will be set to the ith element of 2*x; otherwise,it will be set to 3*x[i], and so on.

The advantage of ifelse() over the standard if-then-else construct is that it is vectorized, thus potentially much faster.

The syntax of if…else statement is:

if (test_expression) {

statement1

} else {

statement2

}

The else part is optional and is only evaluated if test_expression is FALSE.

It is important to note that else must be in the same line as the closing braces of the if statement. Example :

x <- -7

if(x > 0){

print("Non-negative number")

} else {

print("Negative number")

}

Output :

[1] "Negative number"

The above conditional can also be written in a single line as follows.

if(x > 0) print("Non-negative number") else print("Negative number")

This feature of R allows us to write construct as shown below.

> x <- -7

> y <- if(x > 0) 5 else 6

> y

[1] 6

The if…else ladder (if…else…if) statement allows you execute a block of code among more than 2 alternatives

The syntax of if…else statement is:

if ( test_expression1) {

statement1

} else if ( test_expression2) {

statement2

} else if ( test_expression3) {

statement3

} else {

statement4

}

Only one statement will get executed depending upon the test_expressions.Example :

x <- 0

if (x < 0) {

print("Negative number")

} else if (x > 0) {

print("Positive number")

} else

print("Zero")

Output

[1] "Zero"

Like the if-then-else construct found in most languages, R also includes a vectorized version, the ifelse() function. This the short form of the traditional IF Else statement.

>ifelse(b,u,v)

where b is a Boolean vector, and u and v are vectors.

The return value is itself a vector; element i is u[i] if b[i] is true, or v[i] if b[i] is false.

Example:

> x <- 1:10

# %% is the mod operator

> y <- ifelse(x %% 2 == 0,5,12)

> y

[1] 12 5 12 5 12 5 12 5 12 5

Here, we wish to produce a vector in which there is a 5 wherever x is even or a 12 wherever x is odd. So, the actual argument corresponding to the formal argument b is (F,T,F,T,F,T,F,T, F,T). The second actual argument,5, corresponding to u, is treated as (5,5,...)(ten 5s) by recycling. The third argument, 12, is also recycled, to (12,12,...).

Here is another example:

> x <- c(5,2,9,12)

> ifelse(x > 6,2*x,3*x)

[1] 15 6 18 24

We return a vector consisting of the elements of x, either multiplied by 2 or 3, depending on whether the element is greater than 6. The expression x > 6 is a vector of Booleans. If the ith component is true, then the ith element of the return value will be set to the ith element of 2*x; otherwise,it will be set to 3*x[i], and so on.

The advantage of ifelse() over the standard if-then-else construct is that it is vectorized, thus potentially much faster.

**if…else statement**The syntax of if…else statement is:

if (test_expression) {

statement1

} else {

statement2

}

The else part is optional and is only evaluated if test_expression is FALSE.

It is important to note that else must be in the same line as the closing braces of the if statement. Example :

x <- -7

if(x > 0){

print("Non-negative number")

} else {

print("Negative number")

}

Output :

[1] "Negative number"

The above conditional can also be written in a single line as follows.

if(x > 0) print("Non-negative number") else print("Negative number")

This feature of R allows us to write construct as shown below.

> x <- -7

> y <- if(x > 0) 5 else 6

> y

[1] 6

**nested if…else**The if…else ladder (if…else…if) statement allows you execute a block of code among more than 2 alternatives

The syntax of if…else statement is:

if ( test_expression1) {

statement1

} else if ( test_expression2) {

statement2

} else if ( test_expression3) {

statement3

} else {

statement4

}

Only one statement will get executed depending upon the test_expressions.Example :

x <- 0

if (x < 0) {

print("Negative number")

} else if (x > 0) {

print("Positive number")

} else

print("Zero")

Output

[1] "Zero"

*-------------------------------------------------------------------------------------------------------*

Thanks, TAMATAM ; Business Intelligence & Analytics Professional

--------------------------------------------------------------------------------------------------------

Thanks, TAMATAM ; Business Intelligence & Analytics Professional

--------------------------------------------------------------------------------------------------------

## No comments:

## Post a Comment

Hi User, Thank You for visiting My Blog. Please post your open Feedback only related to this Blog Posts. Please note that I cannot respond to the Anonymous Comments.