Helmut ★★★ Vienna, Austria, 2019-04-26 18:03 (1991 d 19:36 ago) Posting: # 20244 Views: 8,392 |
|
Dear R-Users, I can’t get my head around this:
Gives
Why the heck? Quoting the R-Inferno: Curly braces are also useful with — Dif-tor heh smusma 🖖🏼 Довге життя Україна! Helmut Schütz The quality of responses received is directly proportional to the quality of the question asked. 🚮 Science Quotes |
mittyri ★★ Russia, 2019-04-26 18:20 (1991 d 19:20 ago) @ Helmut Posting: # 20245 Views: 7,440 |
|
Dear Helmut, the code is worth a thousand sentences loc.stat <- function(x, type, na.rm, distr) { — Kind regards, Mittyri |
Helmut ★★★ Vienna, Austria, 2019-04-26 18:41 (1991 d 18:58 ago) @ mittyri Posting: # 20247 Views: 7,500 |
|
Hi mittyri, you made my day! Goofy and R-help were not my friends… — Dif-tor heh smusma 🖖🏼 Довге життя Україна! Helmut Schütz The quality of responses received is directly proportional to the quality of the question asked. 🚮 Science Quotes |
d_labes ★★★ Berlin, Germany, 2019-04-27 16:29 (1990 d 21:10 ago) @ mittyri Posting: # 20252 Views: 7,426 |
|
Dear mittyri! May I ask: Whats goin on here? Don't get it. Do you have the thousand sentences for me? — Regards, Detlew |
mittyri ★★ Russia, 2019-04-28 00:23 (1990 d 13:16 ago) @ d_labes Posting: # 20253 Views: 7,438 |
|
Dear Detlew! ❝ Do you have the thousand sentences for me? veryspecialfunction <- function(flag) { Now running: veryspecialfunction(2) [1] "two" veryspecialfunction(1) # nothing here! typeof(veryspecialfunction(1)) [1] "NULL" — Kind regards, Mittyri |
ElMaestro ★★★ Denmark, 2019-04-28 00:34 (1990 d 13:05 ago) @ mittyri Posting: # 20254 Views: 7,479 |
|
Hi all, "smart" lost this round. I always use explicit return(something) in my functions, when they are supposed to return a value regardless of whether it is a list, numeric, NULL or data frame some other thingy.Yes, that involves tapping a few times more on the keyboard but it does solve a lot of problems and best of all, it makes code readable. In language like C you would never run into this phenomenon. You declare explicitly which type the function returns and you will be bombarded with errors or warnings if you fail to return the right type of if you forget the return statement. I find it very comfortable. — Pass or fail! ElMaestro |
d_labes ★★★ Berlin, Germany, 2019-04-28 21:36 (1989 d 16:04 ago) @ ElMaestro Posting: # 20255 Views: 7,332 |
|
Hi Öberster Größter Meister, ❝ "smart" lost this round. Totally. ❝ I always use explicit A good idea. But if you use R you are told that doing so has the penalty of a longer run-time. Bloody Hell! ❝ Yes, that involves tapping a few times more on the keyboard but it does solve a lot of problems and best of all, it makes code readable. Full ACK! ❝ In language like C you would never run into this phenomenon. You declare explicitly which type the function returns and you will be bombarded with errors or warnings if you fail to return the right type of if you forget the return statement. I find it very comfortable. Weakly typed (R) against strong typed language (C or C++). But IIRC has C or C++ also some weaknesses. — Regards, Detlew |
ElMaestro ★★★ Denmark, 2019-04-28 22:36 (1989 d 15:03 ago) @ d_labes Posting: # 20256 Views: 7,346 |
|
Hi d_labes, ❝ But IIRC has C or C++ also some weaknesses. That's right. For example, the number of built in functions in C is small. You need to write most functionality from scratch, so what you can do with three lines of code in R can easily be 100 lines of code in C. Much more if you think of e.g. the plot function which opens up a graphic device and with one line gives you a graph. Horrendously many hours of programming is needed in C to do such a thing. The biggest disadvantage I can think of in C is clearly: Pointers (especially character pointers). They were hard to grasp initially. C does not have strings. The biggest advantage I can think of in C is: Pointers. They speed things up immensely. Like in the bootstrap code for dissolution posted here in this forum some time ago. Infinitely faster than any implementation seen out there. But then again, when you are working on a dossier it usually does not matter for real if you get the output in 1 or 10000 seconds as long as you meet your submission deadline I am not well versed with C++ as I never really had a need for classes/objects. They are not so necessary for numerical purposes. — Pass or fail! ElMaestro |
Helmut ★★★ Vienna, Austria, 2019-04-28 22:50 (1989 d 14:50 ago) @ d_labes Posting: # 20257 Views: 7,488 |
|
Dear Detlew, ❝ ❝ I always use explicit ❝ ❝ A good idea. Yep. ❝ But if you use R you are told that doing so has the penalty of a longer run-time. ❝ Bloody Hell! Does that really matter? library(microbenchmark) ❝ Weakly typed (R) against strong typed language (C or C++). Exactly. You know that I’m facing a similar story with the forum scripts (PHP: weakly typed) generating database-queries (SQL: strongly typed). Bloody scavenger hunt. — Dif-tor heh smusma 🖖🏼 Довге життя Україна! Helmut Schütz The quality of responses received is directly proportional to the quality of the question asked. 🚮 Science Quotes |
ElMaestro ★★★ Denmark, 2019-04-28 23:16 (1989 d 14:23 ago) @ Helmut Posting: # 20258 Views: 7,323 |
|
Hi Hotzi, I even go a step further, perhaps only because I am rather dumb. My functions almost always look like this:
Foo=function(bar) I do that to make sure that whatever happens with pairs of curly braces I know that my function return is captured at the end. When finding errors and bugs I just need to look at rslt and work backwards. print(rslt) immediately before the return statement is often a very good beginning to that. Not sure if this is smarter (in terms of error finding) but it is my solution to a lot of trouble. Possibly just owing to my own lack of skills.In C you can also exit with curly braces in the middle of a function. Debugging that sh!t is impossible for me. — Pass or fail! ElMaestro |
Helmut ★★★ Vienna, Austria, 2019-04-29 01:38 (1989 d 12:01 ago) @ ElMaestro Posting: # 20259 Views: 7,327 |
|
Hi ElMaestro, ❝ My functions almost always look like this: ❝ ❝ Foo=function(bar) ❝ { ❝ ##whatever happens in loops and conditions it sets a variable called e.g. rslt ❝ ❝ } Good practice. I use it in longer functions as well and in ones which become part of a package, always. Only very, very slightly slower: library(microbenchmark) — Dif-tor heh smusma 🖖🏼 Довге життя Україна! Helmut Schütz The quality of responses received is directly proportional to the quality of the question asked. 🚮 Science Quotes |
ElMaestro ★★★ Denmark, 2019-04-29 11:40 (1989 d 02:00 ago) @ Helmut Posting: # 20260 Views: 7,276 |
|
Hi Hötzi, 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). 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? Perhaps space is allocated for both "foo" and "bar" in memory and then there is a decision as to which one to throw away and which one to keep on the CPU stack? I don't have a very good at understanding of these things. This one is appearing even worse:
QWERTY2=function(x) I have no idea why QWERTY2 would not outperform QWERTY or at least be the same if R has a kind of optimiser in the interpreter. Would you happen know???? — Pass or fail! ElMaestro |
Helmut ★★★ Vienna, Austria, 2019-04-29 12:41 (1989 d 00:58 ago) @ ElMaestro Posting: # 20261 Views: 7,295 |
|
Hi ElMaestro, ❝ Interestingly (at least to moi!), I tried out of curiosity to do this: ❝ ❝ ❝ { ❝ } ❝ ❝ 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) Shortended output of res1 and res2 : expr median cld 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 |
ElMaestro ★★★ Denmark, 2019-04-29 14:55 (1988 d 22:44 ago) @ Helmut Posting: # 20262 Views: 7,281 |
|
Hi Hötzi, ❝ Use Try this: library(microbenchmark) note I am passing Len to f1 even though it is not used there in order to give the function call the same overhead, otherwise the comparison might be called unfair. On my system f2 is a lot faster, so the advantage of runif may be solely syntactic and not in any way a true vectorisation advantage (easier but not more efficient). — Pass or fail! ElMaestro |
mittyri ★★ Russia, 2019-04-30 15:05 (1987 d 22:34 ago) @ ElMaestro Posting: # 20263 Views: 7,317 |
|
Hi ElMaestro, please see ifelse internals:
function (test, yes, no) That's why Hadley implemented his own if_else function. Your one if in f2 is substituted to many of in f1. And the last part is two separated actions for 'No' and 'Yes'. So you can write your own ifelse function (for your own purposes) which will be much faster than the built-in And the winner is f3 = function(x, Len) — Kind regards, Mittyri |