ifelseifelse()
March 18, 2026
ifelseifelse()ifif (logical argument){ # e.g., x > 1 or is.character(x)
body
}
if you have to quote it: ?"if"data(rivers) #length of 141 "major" North American rivers (miles) head(rivers)
## [1] 735 320 325 392 524 450
is.numeric(rivers)
## [1] TRUE
hist(log10(rivers), breaks=30) abline(v=log10(median(rivers)),col=3, lwd=3)
ifif (is.numeric(rivers)){
median(rivers)
}
## [1] 425
ifYou can put it all on one line, but…
if (is.numeric(rivers)){ median(rivers) }
## [1] 425
ifif (!is.numeric(rivers)){
median(rivers)
}
! negates the logicalis.numeric(rivers) is TRUE, so !is.numeric(rivers) is FALSEelseif else statements# Pass character object
x<-letters
if (is.numeric(x)){
median(rivers)
} else {
class(x)
}
## [1] "character"
else# Pass numeric object
x<-1:10
if (is.numeric(x)){
median(rivers)
} else {
class(x)
}
## [1] 425
} else { on the same lineThe closing brace of if and the else keyword must be on the same line.
# WRONG -- R thinks the if statement is done
if (is.numeric(x)){
median(rivers)
}
else {
class(x)
}
# RIGHT -- else follows the closing brace
if (is.numeric(x)){
median(rivers)
} else {
class(x)
}
if/else summary so far| Feature | Detail |
|---|---|
| Test | Must evaluate to a single TRUE or FALSE |
| Body | Runs only when test is TRUE |
else |
Optional; runs when test is FALSE |
| Placement | } else { must be on the same line |
| Vectorized? | No – works on a single logical value |
if/else is for scalar decisionsifelse()Single element example
x<-TRUE ifelse(x, 1, 2)
## [1] 1
ifelse(test, TRUE , FALSE)ifelse()Long vector example
lowercase<-TRUE ifelse(lowercase, letters, LETTERS)
## [1] "a"
lowercase<-c(TRUE, FALSE, TRUE, FALSE) ifelse(lowercase, letters, LETTERS)
## [1] "a" "B" "c" "D"
ifelse always the same shape (length usually) as the logicalifelse()river.bin<-ifelse(rivers > median(rivers), "Long", "Short") river.bin[1:10] # Just look at the first 10
## [1] "Long" "Short" "Short" "Short" "Long" "Long" "Long" "Short" "Long" ## [10] "Long"
Make ordered factor:
river.bin<-factor(river.bin, levels= rev(unique(river.bin)), ordered=T) str(river.bin)
## Ord.factor w/ 2 levels "Short"<"Long": 2 1 1 1 2 2 2 1 2 2 ...
if/else vs ifelse() – When to use whichif / else |
ifelse() |
|
|---|---|---|
| Input | Single logical value | Vector of logicals |
| Output | Whatever the body returns | Vector same length as test |
| Use case | Control flow, branching | Reclassifying data, creating new columns |
# Scalar decision if (is.numeric(rivers)) median(rivers) # Vectorized classification ifelse(rivers > median(rivers), "Long", "Short")
for and iffor (each element in a vector){
if(an element meets this condition){
do this stuff
} else {
do this other stuff
}
}
Using a for loop and an if statement, print only the elements of par() with more than three elements.
if).1.Figure out the loop structure.
for(i in par()){
print(i)
}
This prints every element of par(). Now we need to filter.
2.Figure out the conditional (using if).
for(i in par()){
if (length(i) >3){
print(i)
}
}
## [1] 0 1 0 1 ## [1] 1.02 0.82 0.82 0.42 ## [1] 5.1 4.1 4.1 2.1 ## [1] 1 1 1 1 ## [1] 0 0 0 0 ## [1] 0 1 0 1 ## [1] 0 0 0 0 ## [1] 0.1171429 0.9400000 0.1700000 0.8633333 ## [1] 0 1 0 1
if (test){ body } – runs body when test is a single TRUEelse { body } – runs when test is FALSE; must follow } on the same lineifelse(test, yes, no) – vectorized; applies to every elementif/else for control flow (one decision)ifelse() for data transformation (many decisions at once)