4 ms·
This week I got a bit more familiar with R while adding it as a scripting language for a data IDE I'm working on (it already supported other languages). It is
by eatonphil 5y ago
This week I got a bit more familiar with R while adding it as a scripting language for a data IDE I'm working on (it already supported other languages).
It is a very hard language and community to get into! The documentation is very sparse. Library documentation is published as PDF (I guess?) and also very sparse. The default `print` behavior is pretty hard to understand. It's 1-indexed and it took me a while to realize every time I think `array[1]` I should write `array[[1]]`. I can't tell the difference between `<-` and `=`.
My guess is that it was probably a great language at some point but is way behind other numeric scripting languages like Julia or Matlab in terms of community attention and language ergonomics.
I know it's highly used but other than legacy reasons I'm not sure why you'd want to learn it over Julia.
I'm also curious to investigate how the aspects I'm critical of differ in Octave.
Edit: totally fair, 1-indexing shouldn't have been a "critique". Lots of languages do that.
- Dumblydorr 5y agoUse the tidyverse and use the cheatsheets for dplyr and the book R for Data Science. If you're trying to use primarily base R, you'll be limited and hamstrung. Tidyverse is the modern framework of choice.
- mshron 5y agoI highly recommend anybody getting into R to skip the base language (which indeed is ancient and full of gotchas) and go straight for the Tidyverse[1]. You can always go back in and learn how to do things the old way later. Over the last decade, the R community has largely standardized around tools like dplyr, ggplot, tibble, purrr, and so on that make doing data science work way easier to reason about. Much more ergonomic. At my company we switched from using Python to using R for most analytical data science work because the Tidyverse tools make it so much easier to avoid bugs and weird join issues than you get in a more imperative programming environment. [1] https://www.tidyverse.org/ https://www.tidyverse.org/
- jstx1 5y agoI would recommend the opposite - pick stuff from tidyverse (mostly dplyr and ggplot2) only if you need them. Knowing base R goes a long way on its own.
- melling 5y agoPipe operator is now in base R so one of the nice features is now standard |> https://www.r-bloggers.com/2021/05/new-features-in-r-4-1-0/ https://www.r-bloggers.com/2021/05/new-features-in-r-4-1-0/
- tarsinge 5y agoI found the book R for Data Science (which is free http://r4ds.had.co.nz http://r4ds.had.co.nz) to be a very good introduction to R with Tidyverse.
- tpoacher 5y agoI'm on the same page as the other commenter here, except stronger. Avoid tidyverse like the plague, except when you can't, or when you don't actually care about the sanity of your code and are happy copy/pasting pre-prescribed snippets without needing to understand let alone modify them.
- uryga 5y agoi would recommend getting comfortable with doing stuff with base R, then trying tidyverse. Starting with dplyr might get you results quick, but its "special evaluation" actively confuses your understanding of how the base language actually works (speaking from experience with an R course and subsequently helping other confused folks) Consider this example: # base R starwars[starwars$height < 200 & starwars$gender == "male", ] # dplyr starwars %>% filter( height < 200, gender == "male" ) (Source: https://tidyeval.tidyverse.org/sec-why-how.html https://tidyeval.tidyverse.org/sec-why-how.html) Where'd `height` and `gender` come from in the dplyr version? They're just columns in a DF, not variables, and yet they act like variables... Well that's the dplyr magic baby! dplyr (and other tidystuff) achieves this "niceness" by doing a whole bunch of what amounts to gnarly metaprogramming[1] -- that example was taken from a whole big chapter about "Tidy evalutation", describing how it does all this quote()-ing and eval()-ing under the hood to make the "nicer" version work. it's (arguably) more pleasant to read and write, but much harder to actually understand -- "easy, but not simple", to paraphrase a slightly tired phrase. --- [1] IIRC it works something like this. the expressions height < 200 gender == "male" are actually passed to `filter` as unevaluated ASTs (think lisp's `quote`), and then evaluated in a specially constructed environment with added variables like `height` and `gender` corresponding to your dataframe's columns. IIRC this means it can do some cool things like run on an SQL backend (similar to C#'s LINQ), but it's not somthing i'd expose a beginner to.
- canjobear 5y agoMy experience is that this weird evaluation order stuff is only confusing for students with a lot of programming experience who already expect nice lexical scope. For those coming in from Excel, the tidyverse conventions are no problem and are in fact easier than all the pedantic quoting you have to do in something like Pandas. It only gets confusing when you want to write new tidyverse functions, and even then, base R isn’t any simpler: the confusing evaluation order is built into R itself at the deepest level.
- uryga 5y agoEDIT: i gotta admit, you sound like you've got more experience with teaching R than me. so perhaps my opinions here are a bit strong for what they're based on, i.e. tutoring a couple of non-programmer friends and my own learning process. still... > My experience is that this weird evaluation order stuff is only confusing for students with a lot of programming experience who already expect nice lexical scope fair point, but for the most part, R itself does use pretty standard lexical scoping unless you opt into "non-standard evaluation" by using `substitute`[1]. so building a mental model of lexical scoping and "standard evaluation" is a pretty important thing to learn. after that, the student can see how quoting can "break" it, or at least be able to understand a sentence like "you know how evaluation usually works? this is different! but don't worry about it too much for now". and i think dropping someone new straight into tidyverse stuff gets in the way of this process. > and even then, base R isn’t any simpler: the confusing evaluation order is built into R itself at the deepest level. i mean, quoting can't really work without being deeply integrated into the language, can it? besides: - AFAICT base R data manipulation functions don't use it a lot. [2] - for the most part, R's evaluation order can be ignored (at a certain learning stage) because it's not observable if you stick to pure stuff, which you probably should anyway. --- [1] http://adv-r.had.co.nz/Computing-on-the-language.html#capturing-expressions http://adv-r.had.co.nz/Computing-on-the-language.html#captur... [2] admittedly, stuff with `formula`s is similarly wacky, and if you're doing stats you're going to run into that sooner or later...
- mr_toad 5y ago> ggplot One day I’ll have a whole week free so I can sit down and learn an entire graphical grammar so that I can remove the egregious amounts of chart-junk in the ggplot defaults.
- pjmorris 5y agoThe joke about R is that it is great because it was written by statisticians for statisticians and that R is terrible because it was written by statisticians for statisticians. The two best reasons to use R, IMO, are that many statisticians write up their new methods in R, so it is a window into current statistical research and practice [0], and that R is home to ggplot and the rest of the tidyverse (or Hadley-verse), a systematic approach to common data analysis tasks. [0] https://cran.r-project.org/web/packages/available_packages_by_name.html https://cran.r-project.org/web/packages/available_packages_b... [1] https://www.tidyverse.org/ https://www.tidyverse.org/
- samch93 5y agoThis is the typical „CS-people“ reaction to learning R. R is a language written by statisticians for statisticians which can be a good but also a bad thing. Fact is that R is the lingua franca of statistics, most new methods will be first available in R, rarely in python, almost never in julia. Despite the superior design of julia, there are good reasons to still use R, for example, there are many state of the art libraries such as ggplot2 or data.table which beat any alternative from python or julia.
- jhgb 5y agoShouldn't Vega be comparable to ggplot2, at least if you're working with HTML output?
- lanstin 5y agoAnd as far as community support, when I was on the R user listserv, Prof Ripley from Oxford would explain why your question was using the wrong analysis for your data, explain what analysis you should use, shoe the sample code for the right way, and then mockingly show the dumb way you could have your question answered,. Like twenty emails each British morning. I learned a lot about stats from reading it. Not so much R syntax but stats and stats reasoning.
- otabdeveloper4 5y agoEverything you're complaining about is just petty bikeshedding issues. Just learn it, it's more powerful than the alternatives. P.S. I'm a Python person and not at all an R fanboy, but it's undeniably more powerful and versatile for data science tasks than Python or Julia.
- notafraudster 5y agoYou highlight four issues: 1. Documentation is accessible via the interpreter. You can type ?funcname to get documentation or ?libname for the entry point for almost every library, or use the Help tab in RStudio, the most common interpreter. Package documentation is typically hyperlinked text and of a high quality. You can also see syndicated versions of library documentation online in HTML format. Here is for instance, the HTML documentation for the stats library (the built-in library which covers most of the statistical functions you want): https://stat.ethz.ch/R-manual/R-patched/library/stats/html/00Index.html https://stat.ethz.ch/R-manual/R-patched/library/stats/html/0... or rdocumentation.org or really any dozens of web syndicated versions. The PDF version you mentioned is linked from CRAN, the package repository, but is by no means the only entry point for documentation. 2. The print function -- actually not a single function, but rather a commonly implemented S3 method -- is easy to understand if you understand how the S3 object system in R works and how function dispatch works. What it does depends on the class of the object and whether an S3 print method has been implemented for the class of the object. This is true in most languages. If you're looking to something closer to a bare metal print function you should consider cat, but in general I don't find print confusing at all. 3. The subset operators available in R are documented. Because everything is a function in R, you can easily see the documentation by typing ?`[` or ?`[[` -- both have the same documentation page, which describes the essential difference between the two subsetting operators. This is tricky to learn at first but given that the two operators do different things, both desireable in different contexts, it's sort of difficult to argue this is an ergonomics issue and not a user error. If you want a more hands on discussion of the differences, you can try http://adv-r.had.co.nz/Subsetting.html http://adv-r.had.co.nz/Subsetting.html 4. Assignment, similarly, is documented. You can check ?`=` if you have some concerns or read the documentation online here: https://stat.ethz.ch/R-manual/R-patched/library/base/html/assignOps.html https://stat.ethz.ch/R-manual/R-patched/library/base/html/as... The short version is that although <- is idiomatically preferred by style guides, there are basically no contexts where = would do anything different. You may want to be aware of -> and <<- as other assignment operators. The former allows right hand assignment, which is a fun bit of syntax, and the latter overrides the default assignment scope and forces a global which I personally find distasteful. One final note: the inner workings of any R function for which the implementation is in R can be inspected. Simply type the name of the function and press enter to see the source code of the function. A lot of low level stuff is implemented in C, so you'll find a stub function that calls internal things, but for almost anything else, this is a good way to learn how things work. Like, run-length encoding is implemented in the rle function so just type rle and press enter and voila, you see the full implementation. R has a number of core language issues and things that are annoying but the ones you named read like you puttered around for 10 minutes and didn't do the kind of basic homework you need to do to learn a new language. I wouldn't complain about what a bad language Go is because I don't understand the distinction between := and = as assignment operators.
- tpoacher 5y ago> I'm also curious to investigate how the aspects I'm critical of differ in Octave. Personal opinion, but happy to oblige. > It is a very hard language and community to get into! Same. Especially where "Matlab isn't Octave; Octave isn't Matlab" is concerned. Having said that, on stackoverflow at least, the matlab community seems more hostile to octave questions than the other way round. > The documentation is very sparse. Octave is actually fairly well documented, but unfortunately this is spread out significantly between manuals, helpstrings, and esoteric gems hidden as comments in the actual source code. However, this tends to be less of a problem, since often enough an equivalent function is documented in matlab, which is typically somewhat better in the documentation aspect. (octave is pretty good too though). As for R, I think R is actually really well documented; you do kinda have to get used to its documentation format, but once you do there is nothing you'd want to do that you'll find yourself lacking documentation for. (Proper R, that is. Tidyverse is a slightly different issue; but then again Tidyverse isn't R). > Library documentation is published as PDF You can have excellent in-terminal documentation using "?" and "??" (or "help" / "help.search" ). I have never needed to look at external manuals, but, yes, they do exist, typically in PDF form on CRAN. Furthermore, R is very good at accompanying documentation with examples / vignettes/ demos etc. Octave, in theory, also does the same, but in practice I find many functions don't actually provide the demos. Typically they provide an in-doc example though. > The default print behaviour is hard to understand. Indeed. In fact, R seems to have some sort of infatuation with bash commands doing things in R-space, when in fact it would probably have been much more reasonable to leave the bash commands to do bash things. E.g. ls to list variables, rm to remove them, etc. And, yes, 'cat' to effectively print strings on the terminal verbatim, without other markings. Octave is better here, bash-commands are generally identical within octave. 'print' is provided, but basically it's a wrapper to fprintf. > It's 1-indexed. Yes. Yes it is. This is not a bug, it's a feature. Same with octave, and same with julia. 0-indexing makes sense when you're working primarily with structures that depend on offsets (like pointers). 1-indexing is far more appropriate for languages that abstract such offset-based-structures away, and require ordinal, 'human-indexing' logic instead. > it took me a while to realize every time I think `array[1]` I should write `array[[1]]` Perhaps the chosen syntax is rather unfortunate, but Octave effectively uses the exact same logic here. If you have a cell array, you can either index it with () to obtain another cell array structure, OR you can index it with {} to obtain the 'contents' of that cell element. > I can't tell the difference between `<-` and `=` There are two main differences. 1. "<-" is assignment. "=" is "define" and is only valid at 'top level' of a particular scope; as such, its most appropriate use is to define default arguments in a function's signature. You can use it elsewhere, as long as it's toplevel, but you're discouraged from it. 2. Contrary to '=', the '<-' operator can be interpreted in a way that calls an appropriate 'assignment' function (typically denoted as 'functionname<-' when searching for help). E.g. the line "rows(var) <- x" calls the "rows<-" function, which assigns x to var.rows. It does not evaluate rows(var) first, and then assign x to that. > My guess is that it was probably a great language at some point but is way behind other numeric scripting languages like Julia or Matlab in terms of community attention and language ergonomics False. Not sure what else to say about that. Once you start looking you'll be very surprised how active and cutting edge the R ecosystem is. It's just that language-preference seems very compartmentalised within different communities. R happens to be thriving in genomics / psychology crowds, whereas it's virtually unheard of in mainstream CS crowds. > I know it's highly used but other than legacy reasons I'm not sure why you'd want to learn it over Julia. Because, it has very interesting language designs. In fact, having effectively learned Julia first and R second, it became obvious to me that many of the aspects that I liked in Julia were effectively ideas taken from R. In fact, even though Julia is often compared to Matlab due to its superficially similar syntax, Julia is probably far more similar to R than matlab/octave.
- dm319 5y agoIf you've come from an imperative style of programming, you may end up fighting R before figuring out how it works best. It has a beautiful functional style, best demonstrated by tidyverse and map/apply functions. R has the fastest data table / tabular implementation (yes, faster than Julia), which is the standard format for most data and statistical analyses. 1 indexing is standard for numerical languages. The documentation is referenced to papers on the statistical method - I find it usually sufficient, but depends on what package you are talking about.