4 ms·
Lot's of negative comments in here about learning R from experienced programmers. I've found this is largely because experienced programmers have this unjustifi
by baron_harkonnen 5y ago
Lot's of negative comments in here about learning R from experienced programmers. I've found this is largely because experienced programmers have this unjustified bias that R is some toy language that should be easy to learn and has nothing to teach them. If you approached a language like Rust in the same way you would likely be just as frustrated with it.
Certainly R has its quirks, but most of this comes from being one of the oldest continuing existing programming languages there is. It derives from S which was written 46 years ago. Because of this it has multiple object/class systems reflecting the changing standards for OOP. It's most dominant one, S3, predates Java and therefore uses the Generic Function paradigm of OOP similar to Common Lisp's ClOS. If you're experienced but have never worked with non-Java style OOP you're going to be a bit confused.
R's most important feature, which is well worth studying and mastering for any serious programmer, is that it is a completely vectorized programming language. It borrows this style from APL (though is a million times more readable). Every value in R is a vector and for the vast majority of operations the best approach to solve your problem is by thinking in vector operations. This makes simple things like string formatting with `paste` seem like a confusing nightmare, but there is a real logic there. Functions like `ifelse` can seem strange, and writing C-style code in R, while possible will result in horrible performance.
Once you do learn to think in vectors you realize that R isn't just popular in the stats world because most statisticians haven't seen a "real" programming language, but because you can very rapidly iterate on models. Translating mathematical notation into R, for the experience R programmer, is easier than any other language I've worked with by a long shot.
My advice to any experienced programmer approaching R is to have some respect for the language. Most of the frustrations you'll have aren't because R is a bad language, but because you have less experience than you think and learning R well can expand your programming views in a similar way to Haskell.
- ttz 5y agoTo add: in my experience, programmers who denigrate R think of it as a software engineering language. Not all programming languages are meant to be languages for building large scale applications. Programming is not just about building business applications, it's about getting a computer to do things. And R excels at doing statistics and data science. If you keep that mindset, I believe many will find that its an excellent programming language.
- xiaodai 5y agoNSE is what will do experiences programmer's head in. It's an interesting feature.
- auto 5y agoHonestly, I'd argue that the issue I had in my experience in R wasn't that I myself wasn't giving it the respect it deserved, but rather that the course constructors for my degree didn't give it that respect. We were essentially told "Install R Studio, then just copy and paste these library imports and you're good to go". Your description of a vectorized language makes total sense, and that single paragraph is more of an intro to R than we ever got in class. That said, I think the reason this happened with this class in particular is the viewpoint of (what I perceive) as the majority user's of R. Mathematics focused researchers who never learned the language, they just have done enough to get by and don't really appreciate the underpinnings, or the nuances of running an environment on a machine that isn't theres. I can't entirely absolve myself of blame though, once I realized what was happening I should have gone and done some more foundational R learning, but at that point I just wanted to be done with the class.
- hdkrgr 5y agoI wouldn't say this is true for "the majority user's of R" at all. But for "the majority of professors who tangentially use R code in classes on statistics/bioinformatics/economics/finance (anything not explicitly about R and/or Data Science best practices)"? Absolutely. The R code you see in industry (or academic labs where someone cares about modern R) looks vastly different from those script examples in college that are most people's first impression of the language.
- foxes 5y agoI find it highly unlikely that learning R will expand your programming views anywhere near Haskell. Haskell is an advanced functional programming language. Most R stuff seems to be incoherent, hard to verify correctness, hacky. It does not seem built on a solid foundation like Haskell. Truly everything being a vector is not a huge take away. As for "here's just a bunch of examples", well that seems sort of a brute force way to learn something. I agree examples are important, but they are usually to back something up. Having to reverse engineer some fundamental ideas out of just examples is more work. Seems like this is just promoting more hackyness. Seems like its training a neural network instead of actually understanding something.
- CornCobs 5y agoThis sounds like a highly biased perspective of both R, and what it means for a language to be respectable. If you define a language that "will expand your programming views" as one that can verify correctness easily then yeah, R is terrible at that. But so are many languages that are as flexible as R. Would you have the same opinion of FORTH? Or LISP? or TCL? I think these languages definitely count as "hacky" languages and yet they don't seem to draw the same derision as R (in my experience)
- datastoat 5y agoR certainly expanded my programming views! Haskell did too, but the lessons of Haskell didn't stick the way that R's lessons did. Here are some of the things I learnt from R (though they can be found in other languages of course). * Multiple dispatch. Before learning R, I knew about polymorphism in Java and C++, and multiple dispatch in R broadened my mind and turns out to be very handy. * The idea of "frames". In R, when you invoke `lm(height~sex*age, data=mydataframe)`, the first argument (the formula) doesn't get evaluated until the lm command asks it to be evaluated, and lm can set up the "frame" for that evaluation, i.e. the place where variables are looked up, however it likes. In fact, lm sets it up to include variables from both the scope in which you invoked lm, and also from mydataframe. This is what makes R so wonderfully concise for modelling in data science, compared to e.g. Python + pandas. I knew about frames from interactive debuggers, but until R it never occurred to me that the programming language could manipulate them. * "Held" arguments. In R, when you invoke `plot(x, y1+y2)`, it doesn't just evaluate the arguments and then call the plot function -- it leaves the arguments unevaluated, and invokes plot. Plot then (1) decides when to evaluate them, (2) gets access to the language expression `y1+y2`, which means that it can print "y1+y2" on the plot label, (3) it can even define extra variables to include in the scope when y1+y2 gets evaluated. (I knew about held arguments earlier, from Mathematica, but they only clicked when I read the R documentation.) I've read that R is a descendent of Scheme, and that that's where it gets all its "manipulate language expressions" from. I don't know any Scheme, nor Lisp, and I should definitely learn them -- but in the meantime, my experience has been that R's ability to manipulate language expressions is what makes it such a wonderful sweet spot as a data modelling language. I mostly use Python + pandas nowadays, but it feels such a slog in comparison.
- dwrodri 5y agoI have used R a few times now, and I definitely agree with the statement that thinking in vectors is central to writing good R scripts. However, as a computer engineer and performance junkie, its unfortunate that it doesn't get as much attention as other "STEM DSLs" (Julia, MATLAB) when it comes to performance. The same could technically be argued for Python; The current approaches to dealing with high-performance compute workloads either rely on JITing (e.g. Numba, Tensorflow/JAX's XLA) or bridging over to giant binary blobs through the CPython's well-supported C interop.
- teleforce 5y agoThere are few comments here complaining about R performance just like the parent comment. It's a shame that R does not have industrial strength industrial compiler based on this blog article unlike Python [1]. If you want to see the performance benchmark of R against major programming languages for data analysis please check this excellent keynote speech on the R compilation or more accurately on failures of R compilation effort [2]. As mentioned in [1], the main reason people do not focus on providing industrial strength compiler for R is probably because most of R program’s time are spent in the library codes that are written in a compiled language (e.g., C or Fortran). As you can see from [2], even though this is the case (computation intensive being delegated to proper compiled languages), the R programs running time still suffers due to the "impedance mismatch" because of the look up overheads, indirections, etc. Perhaps someone should try to compile, transpile and/or embed R inside D language similar to the efforts provided in [3][4]. D now supports C compiler internally, has DasBetterC and D interface to C++ (DPP) is second to none. Additionally, since D can also perform better than Fortran codes for numerical computing perhaps Fortran codes can eventually be replaced by existing high performance D library like Mir[5]. I can foresee this symbiotic relationship can be beneficial for R and D (R on D?). D can becomes very popular with extra humongous data analysis and statistics libraries from CRAN, and R can get the run time and compilation performance improvement it badly needs. [1]https://www.r-bloggers.com/2021/05/where-are-the-industrial-strength-r-compilers/ https://www.r-bloggers.com/2021/05/where-are-the-industrial-... [2]https://www.youtube.com/watch?v=VdD0nHbcyk4 https://www.youtube.com/watch?v=VdD0nHbcyk4 [3]https://dlang.org/blog/2018/06/20/how-an-engineering-company-chose-to-migrate-to-d/ https://dlang.org/blog/2018/06/20/how-an-engineering-company... [4]https://theartofmachinery.com/2021/01/01/djinn.html https://theartofmachinery.com/2021/01/01/djinn.html [5]http://blog.mir.dlang.io/glas/benchmark/openblas/2016/09/23/glas-gemm-benchmark.html http://blog.mir.dlang.io/glas/benchmark/openblas/2016/09/23/...
- jmcdl 5y agoWhat is the advantage of "thinking in vectors" in R versus "thinking in vectors" using numpy in Python (for example)?
- carljv 5y agoThere’s some overlap, but vectors are essential to the language. Every type of data in R is a vector. There are no scalars, just vectors of length 1. Instead of dictionaries, it’s idiomatic in R to use “lists”, which are vectors of vectors. Data frames are lists (vectors of vectors) constrained to have equal length element vectors (ie columns). Classes are defined as lists with some metadata (stored in a vector) to direct method dispatch. It’s not just vectorizing mathematical operations a la numpy.
- mraza007 5y agoOkay I’m not sure why R is getting a lot of Hate. After all its a programming language that gets the job done and its very popular in finacial industry especially among Risk Modelers, Quants and i have even seen this being used in analytics space within financial industry
- kgwgk 5y ago> S3, predates Java and therefore uses the Generic Function paradigm of OOP similar to Common Lisp's ClOS S3 appeared a few years before Java but there were other OOP languages like C++ around at the time.
- jghn 5y agoS4 is the one that is reminiscent of CLOS. Dylan was explicitly cited as an inspiration [0] [0] There's an old article from Robert Gentleman named something like "S4 objects in 5 pages, more or less" but I can't find it. However, there's a mention of Dylan and CLOS here: https://genomebiology.biomedcentral.com/articles/10.1186/gb-2004-5-10-r80 https://genomebiology.biomedcentral.com/articles/10.1186/gb-... EDIT: Here's the document I was looking to cite: https://www.stat.auckland.ac.nz/S-Workshop/Gentleman/S4Objects.pdf https://www.stat.auckland.ac.nz/S-Workshop/Gentleman/S4Objec...
- kgwgk 5y agoArguably the S3 object system is also "functional" in spirit, even if it's single-dispatch. https://arxiv.org/pdf/1409.3531.pdf https://arxiv.org/pdf/1409.3531.pdf Object-Oriented Programming, Functional Programming and R (John M. Chambers) "Chambers and Hastie (1992), in the discussion of classes and methods, noted that S differed from other OOP languages because of its functional programming style. In fact, this version of functional OOP finessed the resulting distinction from encapsulated OOP in two ways. First, the methods were dispatched according to a single argument, the first formal argument of the generic function in principle. As a result, the methods were unambiguously associated with a single class, as they would be in encapsulated OOP. Methods were actually dispatched on either argument to the usual binary operators, but a number of encapsulated OOP languages do the same, under the euphemism of operator overloading. "Second, the question of whether methods belonged to a class or a function was avoided by not having them belong to either. Methods were assigned as ordinary functions and identified by the pattern of their name: “function.class”. In any case, there were no class objects and generic functions were ordinary functions that invoked UseMethod() to select and call the appropriate method. Neither the function nor the class was able to own the methods."