Conditional statements (else, if) in R

Hello house,

Good morning to you. Please could someone help with the review of the code below as I need it to generate different results as long as each condition is met

"'

X = c(-1.58, 2.7, -0.9)
Y = c(2.61, -5.3, -0.51)

X
Y

if((X > 1) && (Y > 1)) {
kk <- (X + Y)
}
if ((X > 1) && (Y < 1)) {
kk <- 280
}
if((X < 1) && (Y > 1)) {
kk = (X -Y)
}
if((X < 1) && (Y < 1)) {
kk = 0
}

print(kk)

"'


X = c(-1.58, 2.7, -0.9)
Y = c(2.61, -5.3, -0.51)

X
Y

if((X > 1) && (Y > 1)) {
kk <- (X + Y) 
}
 if ((X > 1) && (Y < 1)) {
kk <- 280
}
 if((X < 1) && (Y > 1)) {
 kk = (X -Y) 
}
 if((X < 1) && (Y < 1)) {
 kk = 0
}

kk 


1 Like

G'day @Zakky

first, when typing code, you use the backtics that are on most keyboards above the tab key (I reckon that's the english name) , ` , not ' :slight_smile: I see you fixed while writing my answer :call_me_hand:
Then, is this some kind of homework? What I can tell you is that you have several if statements in sequence. And as you use &&, you are evaluating only the first element of each vector X and Y against the logical statement. (you only evaluate the first element of X, that is -1.58, and the first of Y, 2.61).
Also, as how the loops are written, with no else or anything around, kk will keep the value of the last correct if statement, if there is more than one correct, you will just keep the last one.
cheers

Yeah, you are right. I am new to R as I have been using matlab for my coding.

From the above statement conditions, the generated result is found below. This result is not the expected result. Also, I tried to add ifelse statement but my code is not working. Please could you advice what I need to add or remove from the script.

[1] -1.58 2.70 -0.90
[1] 2.61 -5.30 -0.51
[1] -4.19 8.00 -0.39

I was always very bad with matlab (I used it for the last time probably more than 10 years ago), so I cannot give you any tip regarding similarities and differences between R and matlab. But we have here a home work policy that I suggest you reading and act accordingly to it:

You should explain things and explain us more about what do you want to do...

In good will, I can tell you, that at least for what I do in R, I use ifelse statements in limited situations. In the code you pasted, it could be used, but it would be quite obfuscated. as you would concatenate several ifelse statements inside ifelse statements.
What you are looking for, in my opinion are, if elses, much more elegant (in my weird opinion xD )
something like:

if (condition meets){
    do something
    } else if (another condition meets {
   do other thing
    } else if (another condition meets) {
   do another stuff
}

You are not that far from that :slight_smile:

cheers

Sorry, it's not an assignment nor home work.

1 Like

Sorry mate, then, can you elaborate better what do you want to accomplish? for example, what is the expected right answer?
cheers

X = c(-1.58, 2.7, -0.9)
Y = c(2.61, -5.3, -0.51)

X
Y

if((X > 1) && (Y > 1)) {
kk <- (X + Y)
}
if ((X > 1) && (Y < 1)) {
kk <- 280
}
if((X < 1) && (Y > 1)) {
kk = (X -Y)
}
if((X < 1) && (Y < 1)) {
kk = 0
}

print(kk)

Expected result below

[1] -1.58 2.70 -0.90
[1] 2.61 -5.30 -0.51
[1] -4.19 280 0

Base solution:

X <- c(-1.58, 2.7, -0.9)
Y <- c(2.61, -5.3, -0.51)

temporary_function <- function(p, q)
{
  if ((p > 1) & (q > 1))
  {
    (p + q)
  } else if ((p > 1) & (q < 1))
  {
    280
  } else if ((p < 1) & (q > 1))
  {
    (p - q)
  } else
  {
    0
  }
}

mapply(FUN = temporary_function, X, Y)
#> [1]  -4.19 280.00   0.00

purrr amd dplyr solution

X <- c(-1.58, 2.7, -0.9)
Y <- c(2.61, -5.3, -0.51)

purrr::map2_dbl(.x = X,
                .y = Y,
                .f = ~ dplyr::case_when(((.x > 1) & (.y > 1)) ~ (.x + .y),
                                        ((.x > 1) & (.y < 1)) ~ 280,
                                        ((.x < 1) & (.y > 1)) ~ (.x - .y),
                                        ((.x < 1) & (.y < 1)) ~ 0))
#> [1]  -4.19 280.00   0.00
4 Likes

Great job well done!

As a software engineer I worry about the boundaries. In this example the value of kk is undetermined in the 2 missing cases: ( X== 1) || (Y== 1) For completeness I would recommend that you force kk to do something under these conditions or you will end up using an obsolete value of kk should an undetected bug occur in another part of your code, causing a secondary bug. This class of bug can be very hard and expensive to find if you do not tidy the loose end of logic statements.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.