News for hackers. Stuff that matters.
Re:Why do we use R rather than Excel? by eatonphil on Sunday July 11, 2021 @09:32AM (#27800639)
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.

Re:Why do we use R rather than Excel? by mshron on Sunday July 11, 2021 @09:40AM (#27800703)
I 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/

Re:Why do we use R rather than Excel? by uryga on Sunday July 11, 2021 @12:46PM (#27802402)
i 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)

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.
Re:Why do we use R rather than Excel? by canjobear on Sunday July 11, 2021 @01:49PM (#27803012)
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. 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.
Re:Why do we use R rather than Excel? by uryga on Sunday July 11, 2021 @02:30PM (#27803385)
EDIT: 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#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...

Re:Why do we use R rather than Excel? by canjobear on Sunday July 11, 2021 @08:27PM (#27805674)
It's true that if you write code that is pure and error-free then you will never bump up against R's strangeness.

But try out this bit of base R:

> hello = function(cats, dogs) { return(cats) }

> hello(100, honk) # where honk has never been defined

> hello(100, print("hello!"))

> hello(100)

Re:Why do we use R rather than Excel? by uryga on Monday July 12, 2021 @11:11AM (#27811068)
yeah, the intersection of lazy evaluation and side-effects (incl. errors/exceptions) gets confusing, you definitely have to be there to help the student out of a jam. but i think it's useful to start out pretending R follows strict evaluation (because it's natural[1]) and then, once the student gets their bearings, you can introduce laziness.

---

[1] well, not "natural", but aligned with how math stuff is usually taught/done. in most cases, when asked to evaluate `f(x+1)`, you first do `x+1` and then take `f(_)` of that.

Re:Why do we use R rather than Excel? by buixuanquy on Sunday July 11, 2021 @09:53PM (#27806126)
Wow, now I understand the reasons. As Python guy I'm having trouble to understand how it is possible in R.
Re:Why do we use R rather than Excel? by uryga on Monday July 12, 2021 @05:50AM (#27808668)
https://www.r-bloggers.com/2018/07/about-lazy-evaluation/

tldr: basically, R passes all function arguments as bundles of `(expr_ast, env)` [called "promises"]. normally, they get evaluated upon first use, but you can also access the AST and mess around with it. AFAIK this is called an "Fexpr" in the LISP world.

(originally i had a nice summary, but my phone died mid-writing and i'm not typing all that again, sorry!)

it's very powerful (at the cost of being slow and, i imagine, impossible to optimize). it enables lots of little DSLs everywhere - e.g. lm() from stats, aes() from ggplot2, any dplyr function - which can be both a blessing and a curse.

Re:Why do we use R rather than Excel? by jstx1 on Sunday July 11, 2021 @09:51AM (#27800778)
I 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.
Re:Why do we use R rather than Excel? by melling on Sunday July 11, 2021 @10:17AM (#27801016)
Pipe 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/

Re:Why do we use R rather than Excel? by tarsinge on Sunday July 11, 2021 @10:06AM (#27800907)
I found the book R for Data Science (which is free http://r4ds.had.co.nz) to be a very good introduction to R with Tidyverse.
Re:Why do we use R rather than Excel? by tpoacher on Sunday July 11, 2021 @10:09AM (#27800933)
I'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.

Re:Why do we use R rather than Excel? by mr_toad on Sunday July 11, 2021 @11:43PM (#27806756)
> 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.

Re:Why do we use R rather than Excel? by samch93 on Sunday July 11, 2021 @09:47AM (#27800746)
This 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.
Re:Why do we use R rather than Excel? by lanstin on Sunday July 11, 2021 @01:23PM (#27802781)
And 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.
Re:Why do we use R rather than Excel? by jhgb on Sunday July 11, 2021 @10:02AM (#27800875)
Shouldn't Vega be comparable to ggplot2, at least if you're working with HTML output?
Re:Why do we use R rather than Excel? by pjmorris on Sunday July 11, 2021 @09:41AM (#27800709)
The 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_b...

[1] https://www.tidyverse.org/

Re:Why do we use R rather than Excel? by notafraudster on Sunday July 11, 2021 @10:09AM (#27800927)
You 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/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

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/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.

Re:Why do we use R rather than Excel? by eatonphil on Sunday July 11, 2021 @11:20AM (#27801498)
The syntax issues yeah that was a mistake to include. Although my confusion about = and <- was that I also wasn't getting warnings when using it incorrectly. In Go I cannot mistakenly use = for declaring a variable (without using 'var').

Far more severe is the scant documentation online.

Don't take this as an attack on the language or community. I'm a huge fan of Standard ML and it's arguably in a worse state!

I was interested in supporting R in the first place because I knew of its importance (if only vaguely).

Re:Why do we use R rather than Excel? by wheelinsupial on Sunday July 11, 2021 @03:52PM (#27804063)
Do you mean a lack of documentation on syntax or lack of documentation on statistical methods?

If it’s statistical methods, then you’ll need to look outside of R because R documentation isn’t trying to teach statistical methods.

You can try [1] the series of books teaching statistical methods using R.

In my stats degree we learned R methods alongside the statistical methods. R, to us at the undergrad level, is a fancy calculator. Yes it has functions and can do some “programming,” but it’s purpose is to facilitate using statistical methods and writing up reports.

If the audience of your IDE is programmers who want to do data analysis, then I think that’s a different audience than statisticians, who I think are the majority of users of R. R studio is already a decent IDE that statisticians are familiar with, so it might be a hard group to get to switch.

The syntax in R isn’t great. There are multiple ways of sub setting that depend on the data type you're subsetting.

Many of the top stats programs have notes on R or courses designed to teach R that are freely accessible on the internet.

[1] https://www.routledge.com/Chapman--HallCRC-The-R-Series/book...

Re:Why do we use R rather than Excel? by tylurp on Sunday July 11, 2021 @01:14PM (#27802692)
Really, really confused about your take on documentation. 9/10 you have documentation in the form of a pkgdown site with examples, parameter definitions, change log, articles, etc.

Here’s a good example: https://dplyr.tidyverse.org

Equally confused about your take on the community. R community is sort of a perfect example of an inclusive community actively trying to include everyone with organizations like “rladies” for women in tech.

Re:Why do we use R rather than Excel? by eatonphil on Sunday July 11, 2021 @03:42PM (#27804009)
In the first place, I went looking for information on assignment operators in R. There's no canonical source I can find. There's an ETH edu site that looks pretty old that seems to describe it well, but it's definitely still very minimal: https://stat.ethz.ch/R-manual/R-patched/library/base/html/as.... Is this even the right documentation? I don't know. (I'm not asking you, I'm sharing my thought process.)

Then I went looking for how to interact with JSON. There's no builtin library I guess but rjson seems to be what people use. There's no official documentation I can find on how to install a package but there are many blog posts. The rjson's only official documentation seems to be in PDF and again it's pretty minimal: https://cran.r-project.org/web/packages/rjson/rjson.pdf.

Again, to be fair, anyone looking into Common Lisp or Standard ML or OCaml would probably feel the exact same way about their ecosystems.

Does it cause a problem for existing users? Probably not. Is it the friendliest thing for first-timers to get into? Probably not. Is that a problem? Again probably not?

Re:Why do we use R rather than Excel? by wheelinsupial on Sunday July 11, 2021 @04:11PM (#27804203)
Are you using R or R Studio?

R: https://www.r-project.org/

There are manuals that you can find there.

R Studio (the IDE): https://www.rstudio.com/

R Studio cheat sheets: https://www.rstudio.com/resources/cheatsheets/

There is usually enough information on stack overflow / stack exchange to get through some questions. There is also a stats specific version that can sometimes be helpful. https://stats.stackexchange.com/

You are talking about documentation for user / community contributed packages. There is a minimum amount of standardization that needs to be followed, but yes, I do agree that the documentation could be better.

Hopefully this helps with installing packages: https://jtleek.com/modules/01_DataScientistToolbox/02_09_ins...

Re:Why do we use R rather than Excel? by eatonphil on Sunday July 11, 2021 @04:17PM (#27804244)
Thanks! I did end up figuring everything out this week I was trying to do (basics of syntax, 3rd party libraries, etc.). I posted the original comment just to document my experience on the path to figuring out these basics.
Re:Why do we use R rather than Excel? by tylurp on Sunday July 11, 2021 @04:21PM (#27804272)
Ah, okay agreed. Documentation on base R isn’t great. I was more so coming from the perspective of packages which I realize isn’t the full picture.

Regarding first timers, I think that once they’re aware of RStudio and the content they put out, learning becomes much more friendly and modern.

Still, I definitely agree and would appreciate a modern manual on base R.

Re:Why do we use R rather than Excel? by otabdeveloper4 on Sunday July 11, 2021 @09:54AM (#27800803)
Everything 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.

Re:Why do we use R rather than Excel? by dm319 on Sunday July 11, 2021 @07:00PM (#27805198)
If 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.

Re:Why do we use R rather than Excel? by Dumblydorr on Sunday July 11, 2021 @11:02AM (#27801368)
Use 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.
Re:Why do we use R rather than Excel? by tpoacher on Sunday July 11, 2021 @10:42AM (#27801196)
> 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.