Skip to content

Commit 515a998

Browse files
20250718 - if else conditions
1 parent b03aec0 commit 515a998

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

dataManagement.Rmd

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ mydata$survived == 1
211211
mydata$survived != 1
212212
```
213213

214-
### Greater Than: `>`
214+
## Greater Than: `>`
215215

216216
```{r}
217217
mydata$parch > 1
@@ -234,6 +234,7 @@ mydata$parch >= 1
234234
```{r}
235235
mydata$parch <= 1
236236
```
237+
237238
## Is in a Value of Another Vector: `%in%`
238239

239240
```{r}
@@ -273,6 +274,33 @@ is.na(mydata$prediction)
273274
is.na(mydata$prediction) | mydata$parch >= 1
274275
```
275276

277+
# If...Else Conditions {#ifelse}
278+
279+
```{r}
280+
score <- 15
281+
282+
if(score <= 10){ # check this condition first
283+
rank <- 1
284+
print(rank)
285+
} else if(score <= 20){ # if first condition was not met, check this condition next
286+
rank <- 2
287+
print(rank)
288+
} else{ # if all other conditions were not met, then do this
289+
print("Not Applicable!")
290+
}
291+
```
292+
293+
```{r}
294+
score <- c(1, 10, 20, 40, 100)
295+
296+
rank <- ifelse(
297+
score <= 10, # check this condition
298+
1, # assign this value if true
299+
2) # assign this value if false
300+
301+
rank
302+
```
303+
276304
# Subset {#subset}
277305

278306
To subset a data frame, use brackets to specify the subset of rows and columns to keep, where the value/vector before the comma specifies the rows to keep, and the value/vector after the comma specifies the columns to keep:

0 commit comments

Comments
 (0)