Putting the “fun” in functional programming.
If a randomly generated quantity is above 20, print “that’s a lot”, otherwise print “that’s not a lot.”
quantity <- runif(1, 10,30)
print(quantity)
## [1] 23.51368
if (quantity > 20){
print("that's a lot")
} else {
print("that's not a lot")
}
## [1] "that's a lot"
If a randomly generated quantity is between 10 and 15, print “that’s a little”, if it’s between 15 and 20, print “that’s not a lot”, if between 20 and 25, print “that’s quite a bit”, between 25 and 30, print “that’s a lot!”
quantity <- runif(1, 10, 30)
print(quantity)
## [1] 16.03
if (quantity > 10 & quantity <= 15){
print("that's a little")
} else if (quantity > 15 & quantity <= 20) {
print("that's not a lot")
} else if (quantity > 20 & quantity <= 25) {
print("that's quite a bit")
} else if (quantity > 25) {
print("that's a lot!")
}
## [1] "that's not a lot"
Essentially the same as before, but by including an “else” at the end (without the if), we encompass all other possibilities.
quantity <- runif(1, 10, 30)
cat <- runif(1, 1, 2)
print(c(quantity, cat))
## [1] 26.75845 1.74053
if (quantity > 10 & quantity <= 15 & cat < 1.5){
print("that's a little")
} else if (quantity > 15 & quantity <= 20 & cat < 1.5) {
print("that's not a lot")
} else if (quantity > 20 & quantity <= 25 & cat < 1.5) {
print("that's quite a bit")
} else if (quantity > 25 & cat < 1.5) {
print("that's a lot!")
} else {
print("that's a cat!")
}
## [1] "that's a cat!"
Print numbers from 1 to 75 but if numbers are multiple of 5 print “Foo”, if numbers are multiple of 7 print “Bar” and if numbers are multiple of both 5 and 7 print “FooBar”.
n <- 75
x <- 1:75
i <- 1
for (i in i:n){
if (i %in% seq(0,n, by = 5) & i %in% seq(0, n, by = 7)) {
x[i] <- c("FooBar")
} else if (i %in% seq(0, n, by = 7)) {
x[i] <- c("Bar")
} else if (i %in% seq(0,n, by = 5)) {
x[i] <- c("Foo")
}
}
print(x)
## [1] "1" "2" "3" "4" "Foo" "6" "Bar"
## [8] "8" "9" "Foo" "11" "12" "13" "Bar"
## [15] "Foo" "16" "17" "18" "19" "Foo" "Bar"
## [22] "22" "23" "24" "Foo" "26" "27" "Bar"
## [29] "29" "Foo" "31" "32" "33" "34" "FooBar"
## [36] "36" "37" "38" "39" "Foo" "41" "Bar"
## [43] "43" "44" "Foo" "46" "47" "48" "Bar"
## [50] "Foo" "51" "52" "53" "54" "Foo" "Bar"
## [57] "57" "58" "59" "Foo" "61" "62" "Bar"
## [64] "64" "Foo" "66" "67" "68" "69" "FooBar"
## [71] "71" "72" "73" "74" "Foo"
A few notes on this one. Defining x as an integer vector intially is OK- R will coerce it to a character vector when the loop is run.
Second, this is clearly a sequence problem. A light bulb should go off here. The difficulty with sequences is that i is set equal to a sequence of numbers, while for any given iteration of the loop, i is a single number. So using the %in% (“match”) operator generates a logical TRUE or FALSE statement. Finally, it’s important to list the compound condition first, because otherwise it will be over-ridden by the more frequently occurring single condition (ie. 35 will equal Foo is Foo is first, Bar if Bar is first, FooBar if FooBar is first).