Vous êtes sur la page 1sur 2

Intermediate R course - DataCamp

# Relational Operators
Equality ==
Inequality !=
Greater than; less than > and <
Greater than/equal to; less than/equal to >= and <=

# Relational Operators and Vectors


- Compare vector: The relational operator will be check for every element in the
vector
- TRUE coerces to 1, and FALSE coerces to 1
- Remember that for string comparison, R determines the greater than relationship
based on alphabetical order
- Writing character string in R should use "" sign (i.e., "User", not User)

# Logical Operators
AND & >> only TRUE if both are TRUE, otherwise will yield FALSE
OR | >> TRUE if at least one is TRUE; only FALSE if both FALSE
NOT ! >> put it and read NOT for the code you write, you will know the answer

# Logical Operators and Vectors


- Compare vector: The logical operator will be check for every element in the
vector
- Watch out: 3 < x < 7 to check if x is between 3 and 7 will not work; you'll need
3 < x & x < 7 for that

# Single sign and double sign version (& vs &&, | vs ||)


- in Vector: Double sign logical operator only examine the first element of vector
to conclude as result

# Conditional Statements
A) if statement: if the condition is TRUE, the code will be executed and printed.
Otherwise, it is will not executed nor printed
if(condition) {
expr(expression)
}

Example: This will be printed out because the condition is TRUE


> x <- -3
> if (x < 0) {
print("x is a negative number")
}

B) else statement: the answer if the first condition is not fullfill


if(condition) {
expr1
} else {
expr2
}

Example: the second expression will be printed out


> x <- 5
> if(x < 0) {
print("x is a negative number")
} else {
print("x is either a positive number or zero")
}

C) else if statement: addition of second condition and third expression


if(condition) {
expr1
} else if(condition2) {
expr2
} else {
expr3
}

Example: Whereever the condition is TRUE, the expression following it that will be
printed
> x <- -3
> if(x < 0) {
print("x is a negative number")
} else if(x == 0) {
print("x is zero")
} else {
print("x is a positive number")
}

- You can only use an else statement in combination with an if statement. The else
statement does not require a condition; its corresponding code is simply run if all
of the preceding conditions in the control structure are FALSE
- It's important that the else keyword comes on the same line as the closing
bracket of the if part!
- The else if statement allows you to further customize your control structure. You
can add as many else if statements as you like. Keep in mind that R ignores the
remainder of the control structure once a condition has been found that is TRUE and
the corresponding expressions have been executed.
- Again, It's important that the else if keywords comes on the same line as the
closing bracket of the previous part of the control construct!

You can do anything you want inside if-else constructs. You can even put in another
set of conditional statements. Examine the following code chunk:
if (number < 10) {
if (number < 5) {
result <- "extra small"
} else {
result <- "small"
}
} else if (number < 100) {
result <- "medium"
} else {
result <- "large"
}
print(result)

- look that, results is a new variable we defined above (that why it is use <- )

Vous aimerez peut-être aussi