11 ms·
A Quick Introduction to R
- dm319 5y agoI love R. Once you get it, there is something beautiful about its functional approach. I like using either tidyverse or data.table with pipes, split, map, reduce. The code looks like layers of a filter that data flows through.
- halhen 5y agoAgree! That, and (almost) everything is a vector... Which makes perfect sense for an analytics language. Once I grokked that R became my default language for anything analytics.
- bitcharmer 5y agoIn my domain q/kdb is used extensively. I don't have a decade to master obscure syntax/grammar just for one simple purpose of extracting some data set from a larger population and maybe do some basic statistics on it. If you're like me R is a godsend. You'll also love the tonnes of free packages. You can't get wrong with R if you appreciate simplicity and intuitiveness.
- j7ake 5y agoOnce you include the statistical packages, ggplot2, and dplyr, there is nothing that beats R in ease of prototyping for data exploration, model fits and sanity checks, and data visualisation of high dimensional data.
- funesrequiem 5y agoI don't know if you've heard about it, because it is a relatively recent development, but the tidymodels ecosystem of packages (https://www.tidymodels.org https://www.tidymodels.org) is also breaching the gap from data exploration/visualization to advanced modeling and machine learning in a way that feels really natural if you're used to the tidyverse way of doing things. It's developed by RStudio as the improved version of caret. I've been using it for differential gene expression analysis and it's a game changer in how much time it saves me.
- folli 5y agoWhat about python and its countless packages? (Honest question, I 'grew up' using python in an academic setting, but haven't caught up with the latest developments)
- jhbadger 5y agoAs someone who used ruby (yes real ruby, not rails) before python or R, I definately think R is better for data science and ruby better for everything else. Sadly, I predict a future where python rules over everything.
- FranzFerdiNaN 5y agoPython can of course do the same, its just so much clunkier to do it.
- stewbrew 5y agoPython isn't really an advancement. But it's a more obvious choice for people with a background in software engineering. I have some hopes for Julia though.
- deleted 5y ago[deleted]
- j7ake 5y agoI use python for more data engineering, large scale processing, and PyTorch. In my experience, the specific things R does well, python does it in a clunkier way. Statistical software written by statisticians in academia, bioconductor, and quick prototyping is still much faster in R than in python. My use case is to prototype in R, then move to python if things become more production rather than exploratory.
- fithisux 5y agoTidyverse!
- Dyac 5y agoI've been using https://exploratory.io/ https://exploratory.io/ a lot, which is r in a really nice wrapper where you can do everything point and click, by writing code by hand or a mix.
- deleted 5y ago[deleted]
- _Wintermute 5y agoMy least favourite things about R is its desire to keep on running when it should have errored on something about 50 lines before and happily spitting out some nonsense result - maybe with a warning, often not. One of my previous jobs basically turned into an in-house R consultant for a department in a pharmaceutical company, and I caught so many bugs when investigating some other issue which meant the results people were reporting were completely wrong. A really common one is multiplying 2 vectors of unequal length where broadcasting shouldn't be possible and it just recycles the shorter vector - but hey, it ran without error and there's an output so many researchers don't notice. Not to mention trying to handle errors is pretty miserable, if you want to catch a specific error you have to match the error string, unfortunately the error message changes depending on the locale the R session is running in.
- clove 5y agoSounds like a fun job. How'd you get that position? If you're retiring soon, I'll fill the position for the company.
- fithisux 5y agoThey should have done more for the software engineering side of things because people use it for this reason. For repl driven development or academic code or exercises it is excellent.
- stewbrew 5y agoMaybe these overly self-confident software engineers should just go RTFM.
- funesrequiem 5y agoHi, would it be possible to contact you to ask some career questions related to the pharmaceutical industry and data science? I'm a biostatistician who uses R for everything and lately I've been thinking about doing a career change, but I'm a bit lost with all the available options.
- 5y ago
- scottmcdot 5y agoThe difference between assigning variables via "=" versus "<-" is not mentioned. That would be confusing to someone learning R.
- deleted 5y ago[deleted]
- countrymile 5y ago
- kkoncevicius 5y agoIn practice the difference is almost non-existent, unless you start doing assignments within function calls, which is a popular style among some R stars, like Martin Machler [1]. But on the other hand some of them resolve to just always use "=" everywhere, including one of R's creators - Ross Ihaka [2]. Anyhow, explaining the difference at that part of the tutorial is not easy, so I chose to omit it for now. But might introduce it later, along with "<<-" and "->>", probably after describing closures. [1]: https://github.com/cran/diptest/blob/master/R/dipTest.R#L37 https://github.com/cran/diptest/blob/master/R/dipTest.R#L37 [2]: https://www.youtube.com/watch?v=88TftllIjaY&t=2101s https://www.youtube.com/watch?v=88TftllIjaY&t=2101s
- nomilk 5y ago1^NA is 1, and 2^NA is NA. Bizarre!
- fithisux 5y agoR has many cases of inconsistency. Like substitute "its value is substituted, unless env is .GlobalEnv in which case the symbol is left unchanged." dim and dims :-) R could do more here. I really like R.
- nomilk 5y agoInteresting. I must admit I've never used substitute. I tried dims but: Error in dims(iris) : could not find function "dims" I do find the occasional oddity. I've noticed more very useful messages/warnings (particularly in common tidyverse functions) recently, so I think they help. To be fair, these quirks are generally very uncommon in day to day use.
- nojito 5y agoYou may want to check out substitute2 which is much easier to use. https://rdatatable.gitlab.io/data.table/reference/substitute2.html https://rdatatable.gitlab.io/data.table/reference/substitute...
- sin7 5y ago1*0 = 1 1*1 = 1 1*-1 = 1 2*0 = 1 2*1 = 2 2*-1 = 1/2 When 1 raised to any power equals 1, does the power matter at all? Even if it's unknown, the answer is 1.
- folli 5y agoIf you get started with R, I heavily suggest to use some kind of IDE such as RStudio or Jupyter Notebook. It makes your life so much easier.
- nomilk 5y agoI don't have a source for this, but I think R as a language has one of the highest concentrations of users in a single IDE - and for good reason - something like 80% use the free (and amazing) RStudio IDE.
- funesrequiem 5y agoAnd what would you say the remaining 20% use? Because I've never seen anyone using R outside of RStudio.
- nomilk 5y agoGuessing here: probably Jupyter notebooks, Emacs and vscode, and perhaps the (very minimal) R IDE (if we can call it that) that comes with the installation of base R. I use R directly from the terminal quite a bit for any small jobs, like calculations, purely due to the <1000ms boot time.
- kkoncevicius 5y agoI use R with Vim. Usually the R script file is open on top and there is a :terminal buffer with R running below. And I use a small vim-plugin [1] for sending commands from the editor to the REPL. This has a few advantages, major being that you can run any language with a dynamic REPL this way, without changing your setup. Or, you can even have two files, written in two different languages, open side by side with a corresponding REPLs running beneath each of them. The downside of course is that you miss on auto-completion and other integrations like that. These are not impossible, but you would have to torture your Vim setup quite a bit in order to implement them. [1]: https://github.com/karoliskoncevicius/vim-sendtowindow https://github.com/karoliskoncevicius/vim-sendtowindow
- fithisux 5y agobookdown has a wealth of online books for R.
- huhtenberg 5y ago> Recycling https://github.com/karoliskoncevicius/tutorial_r_introduction/blob/main/README.md#recycling https://github.com/karoliskoncevicius/tutorial_r_introductio... Gotta say this is very elegant.
- curiousgal 5y agoBest thing about R is Shiny
- The_rationalist 5y ago
- streamofdigits 5y agoR is frequently compared with python and julia which are general purpose programming languages but it is not really a proper comparison. Once you approach R as a domain specific language / system then its various quirks and pecularities are more palatable and explainable: they are in a sense the price to pay for tapping a large domain of statistical analysis expertise that is not available elsewhere.
- jstx1 5y agoThis is mental gymnastics. People have some job to do and are looking for an appropriate tool for it; sometimes that’s R and other times it isn’t. Who cares if you call it a DSL or a general purpose language. If I want to do something and the language makes it difficult, telling myself “oh but it’s a DSL” doesn’t get me any closer to solving my problem.
- ineedasername 5y ago>makes it more difficult Yes, sure, as long as you recognize that as a very subjective determination. From the statistician's non-programmer POV the syntax of R or some other language are similarly opaque. Learning one vs. another will present similar investments in time. From their perspective, R does not make things more difficult, and the fact that it's more of the lingua franca within the field has it's own benefits. The people I see complain about R are usually people that learned a different general purpose language first and find that when work requires data analysis they much prefer the GPL for working through the non-analytical portions if their work. (Especially with python where pandas and numpy have made less specialized tasks much easier)
- asdff 5y agoFrom a statisticians POV the R syntax is great. Here is the t test: t.test(x, y = NULL, alternative = c("two.sided", "less", "greater"), mu = 0, paired = FALSE, var.equal = FALSE, conf.level = 0.95, …) A statistician opens the vignette and already knows what all of these variables represent mathematically, and can begin producing analysis immediately.
- jack_squat 5y agoThis is the R resource I recommend: https://www.amazon.com/Using-Introductory-Statistics-Chapman-Hall-dp-1466590734/dp/1466590734/ref=dp_ob_title_bk https://www.amazon.com/Using-Introductory-Statistics-Chapman... Takes a weekend to work through the book and you get a statistics refresher as a bonus.
- zenlf 5y agoOn the contrary, I'm not a fan of R, I'm only a fan of Hadley Wickham and how the Tidyverse and ggplot2's API are designed. They are just incredibly intuitive and easy to use. ggplot2 has fundamentally influenced how I think about plotting. With my limited experience, I have never seen anything like it.
- CornCobs 5y agoSeconded. I was taught ggplot by a great stats professor and the framing of visualizations as a language (gg actually stands for the grammar of graphics!) describing the relation between data and visual elements (layers in the graph) really made something click. The amount of consideration and careful design behind tidyverse APIs (tidyr, ggplot, dplyr) really astounds me. I've never felt the need to actually memorize any of them but they come to me so naturally whenever I type "library(tidyverse)". Very few DSLs, libraries or APIs have ever made me feel this way, and certainly NOT Python and the mess that pandas/matplotlib/scikit is. Even more impressive that he managed to build such a consistent layer atop the hack that is base R. Note that I've nothing against base R. It really appeals to the hacker in me and it certainly has a ton of cool features (a condition system, multiple function evaluation forms - in what other language are `if`, `while`, `repeat` and even parentheses `(` and the BLOCK STATEMENT `{` all implemented as functions?) but damn if it isn't a mess of corner cases and gotchas.
- dm319 5y agoIf it wasn't for some of the lisp-like capabilities of R, you would never have tidyverse EDIT: reference https://news.ycombinator.com/item?id=15869039 https://news.ycombinator.com/item?id=15869039
- jhbadger 5y agoOn the other hand, if it were lispiness that was the issue, surely xlispstat would be the winner. I love xlispstat. I used it in grad school in the 1990s and even maintain the github repository https://github.com/jhbadger/xlispstat https://github.com/jhbadger/xlispstat . But the fact is xlispstat never appealed to the general statistical community and R did.
- upbeat_general 5y agoR reminds me a lot of matlab. Used mainly for compatibility/libraries/ecosystem but still a frustrating interpreted language at its core.
- awild 5y agoMaybe someone can help me with this, how do you integrate r as a cli tool? I'm in a mostly R shop but its integration is so confusing and/or bad with other tools that we usually just rewrite everything in python for integration (which obviously is a huge waste of time). R packages etc have me as an outsider confused,though seem like the obvious choice?
- gompertz 5y agoPersonally I use my programming language of choice to generate a ".r" script and then use the os exec system call of said language to call Rscript scriptname.r... If I'm understanding your question correctly.
- fastaguy88 5y agoYou can always write: Rscript --vanilla your_r_script.r (and #!/usr/bin/env Rscript --vanilla ) Command line arguments are available as: args <- commandArgs(trailingOnly=TRUE) And there are three getopt()-like packages: getopt, optparse, and argparse.
- awild 5y agoWe've done that but someone used relative includes/require statements and everything broke. It's exceptionally annoying.
- eliashaddad 5y agoIn this case you should probably use the "here" or the "rprojroot" packages (libraries in conventional R parlance). They both simplify the usage of relative paths inside a project/repository. If you have a project root with the folders code, data, etc and are running a project on /path/root/code, you can then just call data_dir <- here::here("data") for the data folder, as the here package uses several always to find the root of a project (e.g., looking for a .git folder).
- dm319 5y agoI love R, nothing better for data analysis, stats and plotting. However, if I was making software for other people to use, repeatedly, I would probably pick another language. The R language does have breaking changes, especially in commonly used packages.
- aseerdbnarng 5y agoThis is probably written by a programmer for that reason (and reading the ‘why R is bad’ comments) shows how misunderstood R is by most programmers. Its like giving someone an introduction to the english language by showing them the alphabet and listing punctuation. Yes technically all true, but none of it will stick
- dm319 5y agoYes, there is a lot of R-bashing by people used to imperative languages designed for efficiency in repetitive tasks, not a functional language designed for numerical analysis. The complaints fall into these categories: 1. It's not zero-indexed (even though most numerical languages aren't) 2. Loops are slow (though if you're looping in R you're probably doing it wrong) 3. It's inconsistent 4. The syntax is weird. But people don't talk about the somewhat beautiful functional ability of the language to wrangle data almost magically. Its basis in lisp allows for the tidyverse and data.table to exist[1], and ggplot is a formidable analysis/plotting platform that Python doesn't come close to. [1] https://news.ycombinator.com/item?id=15869039 https://news.ycombinator.com/item?id=15869039
- throwawayboise 5y agoI attended an intro to R workshop and found it very confusing. Being "functional" had nothing to do with it. Inconsistent, yes very much so in my opinion. It felt like a lot of little separately developed tools thrown together into a bundle. But I think mostly my difficulty with R is that I'm not a researcher or statistician. My exposure to and experience with those domains was an undergrad class or two many decades ago. If you don't deeply understand the problem space for which R is intended, you will be lost and confused trying to learn it.
- dm319 5y agoIt's a very different language to imperative languages out there, so it's not surprising that an introductory course would be confusing. There are several ways to do things in R (for example subsetting data, or pulling out elements of structured data), but that doesn't mean it's inconsistent - they are convenience functions. As you say, you have to do some statistics 'in anger' to really get why R is so good. When I've taught introductory sessions on R I focus more on a very short analysis to demonstrate what it is good at.
- Gatsky 5y agoGreat overview. Slight shame it leaves out 'lapply' etc though (and says as much at the top). I just remember realising that you can have lists and run functions on them when I was learning R, and it seemed like a superpower.
- legerdemain 5y agoSaying that R is a domain-specific language for statisticians, and thus its quirks are ignorable, is an incomplete answer. An R program is never just a series of calls to specialized library functions. Programs still need to ingest and emit data, manipulate data ad hoc, take conditional branches based on some runtime condition, and so on. And that glue code must still be written in R. I've had to write a lot of that glue code in R. As someone who mostly writes not-R, my own R irritation comes from a handful of things: - The dot character "." has no semantic meaning in identifiers. It's just a valid character for names. Looking at function names like "is.numeric" really messes with my reading comprehension. - Ambiguously, "." also separates identifiers of objects in one of R's type systems from method calls. In some cases, `foo(bar)` and `bar.foo()` are equivalent. But only in some cases. - Even better, a popular R library defines a function `.()` (i.e., its name is just a single period character), whose job is to expose a surprising quote/unquote expression evaluation semantics. - This is not to mention the special meaning of "." in formula literals, which are fairly ubiquitous in R. - Different authors use different naming conventions. Base prefers "as.numeric," Tidyverse might have "to_factor," another library might prefer camel case. - Finally, R has a surprisingly extensive syntax, exercised by different libraries to different extents, and a correspondingly rich semantics, with "types," "modes," multiple class systems, "expression" objects, immediate and lazy evaluation, expression quoting and unquoting, metaprogramming, and homoiconicity. It is a zoo of a language.