implicit return? [R for BE/BA]

posted by Helmut Homepage – Vienna, Austria, 2019-04-29 10:41 (520 d 19:10 ago) – Posting: # 20261
Views: 4,188

Hi ElMaestro,


» Interestingly (at least to moi!), I tried out of curiosity to do this:
»
» QWERTY=function(x)
» {
»  ifelse ((x==0), "foo", "bar")
» }

»
» 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
[image]

The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes

Complete thread:

Activity
 Admin contact
21,090 posts in 4,398 threads, 1,469 registered users;
online 12 (0 registered, 12 guests [including 4 identified bots]).
Forum time: Thursday 05:51 CEST (Europe/Vienna)

In these days, a man who says a thing cannot be done
is quite apt to be interrupted by some idiot doing it.    Elbert Green Hubbard

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5