TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The Zen of R

84 点作者 mariuskempe大约 13 年前

11 条评论

steve19大约 13 年前
I love R, but it can be frustrating to code with and learn. Some of its datatypes are immensely powerful but work in mysterious ways. It allows you to manipulate expressions in a LISP macro-like fashion which, when used badly by library authors, can make many things appear magical (and inconsistant). There are many inconsistencies in the standard library because of different programming paradigms used (for example (s|m|t|r)apply() vs. Map() and filtering with df$col[] vs. subset() vs. Filter() ).<p>Yet I love writing code in it. So much can be done in so little code. I am always amazed at how little code I write to accomplish a task.<p>The RStudio IDE ( <a href="http://rstudio.org/" rel="nofollow">http://rstudio.org/</a> ) is a very pleasant environment to write code in.
评论 #3716721 未加载
评论 #3716668 未加载
jacobolus大约 13 年前
In some ways this is better, and in others it is much worse. Using named constants instead of magic numbers, and breaking up complex logic into simpler named parts is a huge win for readability/maintainability, as any programmer quickly learns. The big one liner would be a lot nicer in about 3-4 chunks.<p>I’d rewrite this example as something like:<p><pre><code> num.steps &#60;- 1000 num.walks &#60;- 100 step.std.dev &#60;- 0.03 start.value &#60;- 15 rand.row &#60;- function() rnorm(num.steps, 1, step.std.dev) walk &#60;- function () cumprod(rand.row()) * start.value all.walks &#60;- t(replicate(100, walk())) plot(colMeans(all.walks), type = "l") </code></pre> [I’m not an R guy, so that might not be the most typical style ever, but you get the idea...]
评论 #3716093 未加载
评论 #3716261 未加载
评论 #3716470 未加载
svdad大约 13 年前
I've often felt that there is a world of terse, clear code waiting to be unlocked in R, but haven't been able to find it myself. This is a great example.
danielharan大约 13 年前
Had the same experience recently in Octave. Those moments of clarity are a big part of why I write software.
Estragon大约 13 年前
Slightly OT: I'd be really interested to hear a list of specific advantages to using R over using scipy. I have been holding off on learning R for years because scipy has served my purposes pretty well so far, but sometimes I wonder what I'm missing.
评论 #3717005 未加载
评论 #3718180 未加载
评论 #3717055 未加载
choffstein大约 13 年前
Zen? This is madness!<p>In my mind, Matlab's (Octave's) array based programming makes sense. This? This does nothing that I expected it to do!<p>replicate seems to pretty randomly takes a function. Is that an R thing? I think what confuses me most is that there is nothing about this syntax that tells me that "cumprod (rnorm (1000, 1, 0.03))" hasn't already been evaluated! I could not, for the life of me, figure out why replicate didn't just create 100 exact copies. For example, why does replicate(...) evaluate, but the internals don't? This is driving me crazy!
radikalus大约 13 年前
This is slick...but:<p>How often do you want to generate random walks of this type where the variance of the process isn't dependent on its current level?<p>Observe that, as you increase the standard deviation of the random normal (to even .1), your "random walk" always walks to zero.<p>I don't mean to be a beady-eyed-pterodactyl but, as cool as clever one-liners sometimes are, often they solve toy problems. I say this as someone who loves R and uses it everyday and constantly is forced to brute-force with ugly inlined Rcpp code. (Which is fine)
评论 #3716791 未加载
psb217大约 13 年前
In Matlab, where one is generally compelled (for better or worse) to think in terms of array operations, the obvious code would be:<p>plot(mean((cumprod(randn(100,1000) .* 0.03) .* 15))<p>Well, that is assuming one wants a line plot of the mean value of the "location" at each time point across the population of walks. Personally, I find R's rather idiosyncratic approaches to data handling and function wrangling a bit hard to digest.
mbq大约 13 年前
The most of this Zen comes from array paradigm, not functional; still it is nice to see another soul on the path to Renlightenment (-;
评论 #3716039 未加载
swah大约 13 年前
Those moments, its been a while since I've had one of those. (Must open compiler book)
the_cat_kittles大约 13 年前
How did that take a couple months? Even in R's slightly awkward language, random walks shouldn't be very difficult! I'm baffled, I must be missing something.
评论 #3716072 未加载
评论 #3716063 未加载