implicit return? [🇷 for BE/BA]
Hi ElMaestro,
On any system.
Use
Try this:
Shortended output of
Hey, were are the warnings coming from? Try
❝ Interestingly (at least to moi!), I tried out of curiosity to do this:
❝
❝ QWERTY=function(x)
❝ {
❝ }
❝
❝ The benchmark for this version is much, much slower than any of the other proposals (at least on my system).
On any system.
❝ Another example that shows that condensing code towards fewer keystrokes is not always fastest. I wonder what goes on internally since this is much slower?
Use
ifelse()
for vectorized conditions. See this thread.Try this:
library(microbenchmark)
fun1 <- function(x, cond, print=FALSE) {
if (x == cond) {
"foo"
if (print) cat("foo\n")
} else {
"bar"
if (print) cat("foo\n")
}
}
fun2 <- function(x, cond, print=FALSE) {
if (x == cond) {
if (print) cat("foo\n")
return("foo")
} else {
if (print) cat("bar\n")
return("bar")
}
}
fun3 <- function(x, cond, print=FALSE) {
if (x == cond) {
res <- "foo"
} else {
res <- "bar"
}
if (print) cat(res, "\n")
return(res)
}
fun4 <- function(x, cond, print=FALSE) {
ifelse (x == cond, "foo", "bar")
}
fun5 <- function(x, cond, print=FALSE) {
ifelse ((x == cond), res <- "foo", res <- "bar")
if (print) cat(res, "\n")
return(res)
}
fun6 <- function(x, cond, print=FALSE) {
ifelse (x == cond, res <- "foo", res <- "bar")
if (print) cat(res, "\n")
return(res)
}
res1 <- microbenchmark(fun1(round(runif(1, 0, 1), 0), 0),
fun2(round(runif(1, 0, 1), 0), 0),
fun3(round(runif(1, 0, 1), 0), 0),
fun4(round(runif(1, 0, 1), 0), 0),
fun5(round(runif(1, 0, 1), 0), 0),
fun6(round(runif(1, 0, 1), 0), 0),
times=1000L)
res2 <- microbenchmark(fun1(round(c(runif(1, 0, 1), runif(1, 1, 2)), 0),
c(1, 2)),
fun2(round(c(runif(1, 0, 1), runif(1, 1, 2)), 0),
c(1, 2)),
fun3(round(c(runif(1, 0, 1), runif(1, 1, 2)), 0),
c(1, 2)),
fun4(round(c(runif(1, 0, 1), runif(1, 1, 2)), 0),
c(1, 2)),
fun5(round(c(runif(1, 0, 1), runif(1, 1, 2)), 0),
c(1, 2)),
fun6(round(c(runif(1, 0, 1), runif(1, 1, 2)), 0),
c(1, 2)),
times=1000L)
print(res1)
print(res2)
Shortended output of
res1
and res2
: expr median cld
fun1() 2.415 a
fun2() 2.415 a
fun3() 2.416 a
fun4() 3.623 a
fun5() 3.624 a
fun6() 3.624 a
expr median cld
fun1() 54.0350 b
fun2() 53.7340 b
fun3() 54.0360 b
fun4() 12.2265 a
fun5() 12.6800 a
fun6() 12.3780 a
There were 50 or more warnings (use warnings() to see the first 50)
Hey, were are the warnings coming from? Try
fun1()
to fun3()
with a vector-condition and print=TRUE
.—
Dif-tor heh smusma 🖖🏼 Довге життя Україна!
Helmut Schütz
The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes
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:
- R Inferno Helmut 2019-04-26 16:03 [🇷 for BE/BA]
- full conditions! mittyri 2019-04-26 16:20
- full conditions! Helmut 2019-04-26 16:41
- full conditions! d_labes 2019-04-27 14:29
- magik of R implicit return mittyri 2019-04-27 22:23
- magik of R implicit return ElMaestro 2019-04-27 22:34
- implicit return? d_labes 2019-04-28 19:36
- implicit return? ElMaestro 2019-04-28 20:36
- implicit return? Helmut 2019-04-28 20:50
- implicit return? ElMaestro 2019-04-28 21:16
- implicit return? Helmut 2019-04-28 23:38
- implicit return? ElMaestro 2019-04-29 09:40
- implicit return?Helmut 2019-04-29 10:41
- implicit return? ElMaestro 2019-04-29 12:55
- built-in ifelse mittyri 2019-04-30 13:05
- implicit return? ElMaestro 2019-04-29 12:55
- implicit return?Helmut 2019-04-29 10:41
- implicit return? ElMaestro 2019-04-29 09:40
- implicit return? Helmut 2019-04-28 23:38
- implicit return? ElMaestro 2019-04-28 21:16
- implicit return? d_labes 2019-04-28 19:36
- magik of R implicit return ElMaestro 2019-04-27 22:34
- magik of R implicit return mittyri 2019-04-27 22:23
- full conditions! mittyri 2019-04-26 16:20