Difference Between If() and Ifelse() Functions

Difference between if() and ifelse() functions

The warning message:

  the condition has length > 1 and only the first element will be used

tells you that using a vector in if condition is equivalent to use its first element :

[if (v == 1)] ~ [if (v[1] == 1)] ## v here is a vector

You should use the vectorized ifelse. For example you can write your condition like this:

create_dummies<-function(data, categorical_preds){
## here I show only the first condition
data$setosa_flg <-
ifelse (categorical_preds=="setosa",1,0)
data
}

else if(){} VS ifelse()

The if construct only considers the first component when a vector is passed to it, (and gives a warning)

if(sample(100,10)>50) 
print("first component greater 50")
else
print("first component less/equal 50")

The ifelse function performs the check on each component and returns a vector

ifelse(sample(100,10)>50, "greater 50", "less/equal 50")

The ifelse function is useful for transform, for instance. It is often useful to
use & or | in ifelse conditions and && or || in if.

What are the differences between if, else, and else if?


**IF** you are confused
read the c# spec
**ELSE IF** you are kind of confused
read some books
**ELSE**
everything should be OK.

:)

Difference between two 'if' statements and an 'if/else' loop

The difference here is that in your first example, you have two if statements while in the second, you have one if-else statement. Since your for loop doesn't have any braces, it only contains the first statement after it, so properly nested, they're like this:

for
if

if


for
if-else

In the first example, the second if would execute after the loop was all done, if it finished. In the second example, the entire if-else executes on every iteration of the loop.

The first loop iterates until it finds a character with the value x or reaches the end of the array, while the second one returns immediately, because the first character is either x or it isn't.

In actuality, you don't need two conditions. You can properly write your function like this:





function hasX(s) {


for (var i = 0; i < s.length; i++) {

if (s[i] === 'x') {

return true;

}

}


// didn't find an x

return false;

}


console.log(hasX('I play the xylophone'));


console.log(hasX('I play the piano'));

Is there any difference between using multiple if statements and else if statements?

Yes, potentially. Consider this (C#, Java, whatever):

int x = GetValueFromSomewhere();

if (x == 0)
{
// Something
x = 1;
}
else if (x == 1)
{
// Something else...
}

vs this:

int x = GetValueFromSomewhere();

if (x == 0)
{
// Something
x = 1;
}
if (x == 1)
{
// Something else...
}

In the first case, only one of "Something" or "Something else..." will occur. In the second case, the side-effects of the first block make the condition in the second block true.

Then for another example, the conditions may not be mutually exclusive to start with:

int x = ...;

if (x < 10)
{
...
}
else if (x < 100)
{
...
}
else if (x < 1000)
{
...
}

If you get rid of the "else" here, then as soon as one condition has matched, the rest will too.

ifelse() and if else given different results in dplyr mutate() for a time variable

Apparently, you do not understand ifelse. It is fundamentally different from if and else. The documentation clearly says "ifelse returns a value with the same shape as test" which is a vector of length one in your example. mutate then recycles this.

Here is a simple example:

all(c(TRUE, TRUE))
#[1] TRUE
ifelse(all(c(TRUE, TRUE)), 1:2, 3:4) #test is vector of length 1
#[1] 1
ifelse(c(TRUE, FALSE), 1:2, 3:4) #test is vector of length 2
#[1] 1 4

I'd encourage you to study the source code of the ifelse function, which should make it obvious why it behaves like this.

Performance difference of if if vs if else if

Assuming simple types (in this case, I used int) and no funny business (didn't redefine operator= for int), at least with GCC 4.6 on AMD64, there is no difference. The generated code is identical:

0000000000000000 <case_1>:                                   0000000000000040 <case_2>:
0: 85 ff test %edi,%edi 40: 85 ff test %edi,%edi
2: 74 14 je 18 <case_1+0x18> 42: 74 14 je 58 <case_2+0x18>
4: 83 ff 01 cmp $0x1,%edi 44: 83 ff 01 cmp $0x1,%edi
7: 74 27 je 30 <case_1+0x30> 47: 74 27 je 70 <case_2+0x30>
9: 83 ff 02 cmp $0x2,%edi 49: 83 ff 02 cmp $0x2,%edi
c: 74 12 je 20 <case_1+0x20> 4c: 74 12 je 60 <case_2+0x20>
e: 66 90 xchg %ax,%ax 4e: 66 90 xchg %ax,%ax
10: f3 c3 repz retq 50: f3 c3 repz retq
12: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1) 52: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
18: 31 c0 xor %eax,%eax 58: 31 c0 xor %eax,%eax
1a: e9 00 00 00 00 jmpq 1f <case_1+0x1f> 5a: e9 00 00 00 00 jmpq 5f <case_2+0x1f>
1f: 90 nop 5f: 90 nop
20: 31 c0 xor %eax,%eax 60: 31 c0 xor %eax,%eax
22: e9 00 00 00 00 jmpq 27 <case_1+0x27> 62: e9 00 00 00 00 jmpq 67 <case_2+0x27>
27: 66 0f 1f 84 00 00 00 nopw 0x0(%rax,%rax,1) 67: 66 0f 1f 84 00 00 00 nopw 0x0(%rax,%rax,1)
2e: 00 00 6e: 00 00
30: 31 c0 xor %eax,%eax 70: 31 c0 xor %eax,%eax
32: e9 00 00 00 00 jmpq 37 <case_1+0x37> 72: e9 00 00 00 00 jmpq 77 <case_2+0x37>
37: 66 0f 1f 84 00 00 00 nopw 0x0(%rax,%rax,1)
3e: 00 00

The extra instruction at the end of case_1 is just for padding (to get the next function aligned).

This isn't really surprising, figuring out that p isn't changed in that function is fairly basic optimization. If p could be changed (e.g., passed-by-reference or pointer to the various do_… functions, or was a reference or pointer itself, so there could be an alias) then the behavior is different, and of course the generated code would be too.

If-else statement in R to describe three different cases

The else-if construction should be on the same line as the last brace of the preceding block.

The ifelse function is probably not what you're looking for if you're looking to execute multiple statements.

if (x<y) {
...
} else if (x>y) {
...
} else if (x=y) {
...
}

Syntax for ifelse() Function in R Please

You can using between and case_when from dplyr package as:

 library(dplyr)

old_images <- old_images %>% mutate(Quarter = case_when(
between(difference, 1, 279) ~ 1,
between(difference, 280, 558) ~ 2,
between(difference, 559, 837) ~ 3,
between(difference, 838, 115) ~ 4,
TRUE ~ 4
))


Related Topics



Leave a reply



Submit