I've been trying to figure this out all day but to no avail. I have an if statement that is meant to satisfy four possible conditions.
A, B, C are dataframes.
Here is my code:
if (!exists("A") & exists("B")) {
C= B}
else if (exists("A") & !exists("B")) {
C= A}
else if (exists("A") & exists("B")) {
C= rbind(B,A)}
else {C <- NULL}
I keep getting an error on unexpected "}" and unexpected "else". I've followed several examples but still facing this challenge. Any pointers would be much appreciated. Thx.
The if-else statements can be nested together to form a group of statements and evaluate expressions based on the conditions one by one, beginning from the outer condition to the inner one by one respectively.
Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.
Multiple conditions can also be combined using which() method in R. The which() function in R returns the position of the value which satisfies the given condition.
You can put a for loop inside an if statement using a technique called a nested control flow. This is the process of putting a control statement inside of another control statement to execute an action. You can put an if statements inside for loops.
try this
if (!exists("A") & exists("B")) {
C= B
} else if (exists("A") & !exists("B")) {
C= A
} else if (exists("A") & exists("B")) {
C= rbind(B,A)
} else {C <- NULL}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With