Nasty beast [Software]
❝ I made multiple attempts with "if" and conditions, and got multiple different error messages. ElMaestro gave me some hints, resulting each time in a "oh yes of course" reaction and more error messages... I probably misinterpreted the hints.
Using
if()
, its relatives, and loop-constructs (for(), while(), repeat()
) in many cases are prone to errors – as you experienced – and can be slooow. If you have a vector of data (say y
) there a various ways to access its values. Example with 25 random integers in the range 1…40:n <- 25
y <- round(runif(n = n, min = 1, max = 40))
th <- 10 # threshold
n <- length(y) # we know but useful later
head(y, 10) # the first 10
y[1:10] # same
tail(y, 10) # the last 10
y[(n - 9):n] # same but tricky
blq <- which(y < th) # the ones are below the threshold
length(blq) # how many?
y[blq] # show them
y[y < th] # same
which()
is an overkill, though one can include many conditions which [sic] makes the code more easy to understand. If you have two vectors (say x
, y
) like in my last post, you can select values of x
depending on which(y = condition)
.Let’s check:
n <- 50
x <- runif(n = n, min = 1, max = 20)
a <- 0.5
b <- 2
y <- a + b * x + rnorm(n = length(x), mean = 0, sd = 2)
th <- 10
fun1 <- function(x, y, th) { # clumsy
x.th <- numeric()
y.th <- numeric()
bql <- 0L
for (k in seq_along(x)) {
if (y[k] < th) {
bql <- bql + 1
x.th[bql] <- x[k]
y.th[bql] <- y[k]
}
}
z <- data.frame(x.th, y.th)
return(invisible(z))
}
fun2 <- function(x, y, th) { # better
blq <- which(y < th)
x.th <- x[blq]
y.th <- y[blq]
z <- data.frame(x.th, y.th)
return(invisible(z))
}
fun3 <- function(x, y, th) { # a little bit confusing for beginners
x.th <- x[y < th]
y.th <- y[y < th]
z <- data.frame(x.th, y.th)
return(invisible(z))
}
res1 <- fun1(x, y, th)
res2 <- fun2(x, y, th)
res3 <- fun3(x, y, th)
identical(res1, res2); identical(res1, res3) # same?
[1] TRUE
[1] TRUE
What about speed?
library(microbenchmark)
res <- microbenchmark(fun1(x, y, th),
fun2(x, y, th),
fun3(x, y, th), times = 1000L)
print(res, signif = 4)
Unit: microseconds
expr min lq mean median uq max neval cld
fun1(x, y, th) 173.9 181.1 196.1 186.3 189.9 1530 1000 b
fun2(x, y, th) 163.0 170.3 183.6 175.4 179.0 3311 1000 a
fun3(x, y, th) 163.0 169.0 185.5 174.5 178.4 3310 1000 ab
fun2()
shows its strengths.If one has more conditions any external construct will suck. We have 100 random integers (1…50) and want to get the even ones between 20 and 30 in increasing order.
x <- round(runif(n = 100, min = 1, max = 50))
x
[1] 37 29 8 34 6 13 47 22 30 44 6 14 33 18 12 37 32
[18] 10 6 37 27 2 43 40 7 5 47 4 32 17 7 50 39 36
[35] 38 48 34 5 21 43 34 50 29 20 33 6 45 32 28 8 1
[52] 26 29 19 42 9 38 31 25 4 1 23 37 31 2 26 29 24
[69] 40 43 17 16 41 17 5 17 36 16 7 5 36 30 5 8 19
[86] 40 42 30 33 21 13 25 21 33 16 7 33 36 19 37
sort(x[which(x >= 20 & x <= 30 & x %%2 == 0)])
[1] 20 22 24 26 26 28 30 30 30
sort(x[x >= 20 & x <= 30 & x %%2 == 0])
[1] 20 22 24 26 26 28 30 30 30
if() { do this } else { do that } elseif { oops }
.Dif-tor heh smusma 🖖🏼 Довге життя Україна!
Helmut Schütz
The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes
Complete thread:
- Spreadsheet failures, any recent examples? ElMaestro 2019-07-18 13:43 [Software]
- Spreadsheet failures, any recent examples? Ohlbe 2019-07-18 14:32
- Spreadsheet addiction Helmut 2019-07-18 16:12
- Nasty beast Ohlbe 2019-07-18 17:30
- Nasty beastHelmut 2019-07-18 20:26
- Nasty beast Ohlbe 2019-07-19 11:32
- Nasty beast ElMaestro 2019-07-19 12:32
- Decidedly off topic Ohlbe 2019-07-19 13:38
- OT: R limbo 101 Helmut 2019-07-19 13:00
- Nasty beast ElMaestro 2019-07-19 12:32
- Nasty beast Ohlbe 2019-07-19 11:32
- Nasty beastHelmut 2019-07-18 20:26
- Spreadsheet addiction Shuanghe 2019-07-18 18:59
- Spreadsheet addiction Helmut 2019-07-18 20:04
- OT: Spreadsheet addiction Shuanghe 2019-07-19 12:41
- OT: Spreadsheet addiction Helmut 2019-07-19 14:29
- OT: Spreadsheet addiction nobody 2019-07-19 15:53
- OT: Spreadsheet addiction Helmut 2019-07-19 19:32
- OT: Spreadsheet addiction nobody 2019-07-19 15:53
- OT: Spreadsheet addiction Helmut 2019-07-19 14:29
- OT: Spreadsheet addiction Shuanghe 2019-07-19 12:41
- Spreadsheet addiction Helmut 2019-07-18 20:04
- Nasty beast Ohlbe 2019-07-18 17:30
- Spreadsheet etc. failures zizou 2019-07-20 00:18
- As designed ☺ Helmut 2019-07-20 02:12
- As designed ☺ ElMaestro 2019-07-20 09:08
- To round or not to round… Helmut 2019-07-20 12:53
- To round or not to round… ElMaestro 2019-07-20 19:59
- floating-point math is always more complex than you think it is mittyri 2019-07-20 22:43
- To round or not to round… ElMaestro 2019-07-20 19:59
- To round or not to round… Helmut 2019-07-20 12:53
- As designed ☺ ElMaestro 2019-07-20 09:08
- As designed ☺ Helmut 2019-07-20 02:12
- Spreadsheet addiction Helmut 2019-07-18 16:12
- Spreadsheet failures, any recent examples? Ohlbe 2020-12-10 18:43
- Floating point arithmetic, again Helmut 2020-12-10 19:12
- Floating point arithmetic, again ElMaestro 2020-12-10 19:40
- Floating point arithmetic, again Ohlbe 2020-12-10 20:18
- Floating point arithmetic, again ElMaestro 2020-12-10 21:38
- Floating point arithmetic, again Ohlbe 2020-12-10 21:46
- Floating point arithmetic, again ElMaestro 2020-12-10 22:05
- Floating point arithmetic, again Ohlbe 2020-12-10 21:46
- Floating point arithmetic, again ElMaestro 2020-12-10 21:38
- Floating point arithmetic, again Ohlbe 2020-12-10 20:18
- Floating point arithmetic, again Ohlbe 2020-12-10 20:13
- Floating point arithmetic, again ElMaestro 2020-12-10 19:40
- From bad to worse Ohlbe 2020-12-10 22:11
- From bad to worse mittyri 2020-12-11 00:22
- All is good Helmut 2020-12-11 00:36
- Float is float PharmCat 2020-12-18 20:53
- Float is float! Helmut 2020-12-20 23:27
- rational solution in R mittyri 2020-12-21 13:49
- related stuff Helmut 2021-01-14 12:53
- related stuff SDavis 2021-02-09 12:02
- related stuff ElMaestro 2021-02-09 19:55
- related stuff SDavis 2021-02-09 12:02
- related stuff Helmut 2021-01-14 12:53
- rational solution in R mittyri 2020-12-21 13:49
- Float is float! Helmut 2020-12-20 23:27
- Float is float PharmCat 2020-12-18 20:53
- Floating point arithmetic, again Helmut 2020-12-10 19:12
- Spreadsheet failures, any recent examples? Ohlbe 2019-07-18 14:32