-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathif-statements.qmd
121 lines (94 loc) · 2.55 KB
/
if-statements.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
---
title: "Using `if` statements"
---
Conditional statements are fundamental in programming, allowing for decision-making based on different conditions. In R, the primary conditional statement is the `if` statement, which can be used alone or combined with `else` and `else if` for more complex logic.
## Basic If Statement
The basic `if` statement checks a condition and executes a block of code if the condition is true.
**Syntax:**
```r
if (condition) {
# code to execute if the condition is true
}
```
**Example:**
```r
x <- 10
if (x > 5) {
print("x is greater than 5")
}
```
## If-Else Statement
To execute a block of code when the condition is false, use an `else` statement.
**Syntax:**
```r
if (condition) {
# code to execute if the condition is true
} else {
# code to execute if the condition is false
}
```
**Example:**
```r
x <- 3
if (x > 5) {
print("x is greater than 5")
} else {
print("x is not greater than 5")
}
```
### If-Else If-Else Statement
For multiple conditions, use `else if` to check additional conditions.
**Syntax:**
```r
if (condition1) {
# code to execute if condition1 is true
} else if (condition2) {
# code to execute if condition2 is true
} else {
# code to execute if none of the conditions are true
}
```
**Example:**
```r
x <- 7
if (x > 10) {
print("x is greater than 10")
} else if (x > 5) {
print("x is greater than 5 but less than or equal to 10")
} else {
print("x is 5 or less")
}
```
## Nested If Statements
You can also nest if statements inside one another to check multiple levels of conditions.
**Example:**
```r
x <- 15
y <- 20
if (x > 10) {
if (y > 15) {
print("x is greater than 10 and y is greater than 15")
} else {
print("x is greater than 10 but y is not greater than 15")
}
} else {
print("x is 10 or less")
}
```
### Vectorized If Statements
When working with vectors, `ifelse` is a more efficient way to apply conditional logic.
**Syntax:**
```r
ifelse(test, yes, no)
```
- `test`: A logical condition.
- `yes`: The value to return if the condition is true.
- `no`: The value to return if the condition is false.
**Example:**
```r
x <- c(2, 7, 5, 10)
result <- ifelse(x > 5, "Greater than 5", "5 or less")
print(result)
```
## Conclusion
Using if statements in R allows you to execute code based on conditions, making your programs more dynamic and flexible. Whether you're using a simple `if` statement, adding `else` and `else if` for more complex logic, or applying conditional logic to vectors with `ifelse`, mastering these constructs is essential for effective programming in R.