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
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 ' I see you fixed while writing my answer
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.
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
}
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.