Reading — step 1 of 5
Learn
~1 min readControl Flow
x <- 10
if (x > 0) {
cat("positive\n")
} else if (x < 0) {
cat("negative\n")
} else {
cat("zero\n")
}
Logical operators: && (and), || (or), ! (not). Use double && and || for SCALAR conditions inside if. Single & and | are vectorized — they return a vector of TRUE/FALSE.
if (x > 0 && x < 100) { ... } # scalar — what you want for if()
ages > 18 & ages < 65 # vector — for filtering
ifelse(test, yes, no) is the vectorized if — works element-wise:
ages <- c(15, 30, 50, 70)
ifelse(ages >= 18, "adult", "minor")
# "minor" "adult" "adult" "adult"
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…