Notes on learning R

R is a programming language that is specifically designed for statistics. It has many appealing features: it is powerful, flexible, and widely used in the statistical community. The aspects that make R so powerful and flexible, however, contribute to a learning curve that is relatively steeper than what you might find in point-and-click packages like SPSS. The aim of this guide is to provide a general introduction to interacting with R that will reduce the feelings of frustration and helplessness that can emerge early in one’s relationship with it. Although this guide doesn’t assume any preexisting knowledge, if you’ve had no experience with computer programming there may be some parts of it that lack a particularly deep meaning. Try your best to understand them now, but you will also likely benefit from returning to these workshops periodically as you become more comfortable.

The most important R skill: getting help

The most important skill to cultivate up front is the ability to help yourself when you are stuck.

To some degree, R can be helpful in this regard. When you run a command, you find out immediately whether it worked–if something goes wrong, R prints an error (AKA it didn’t run) or a warning (AKA it ran, but thinks you didn’t mean what you did) into your console (see Workshop 1 for what the “console” is). Because the feedback is immediate and costs you nothing, “tinkering” can be a valid approach to learning R: change one small part of your code at a time, re-run it, and see how the output changes or whether it solves the issue you ran into. Do this liberally as you work through these workshops and their practice problems, making small modifications to the examples we provide until you feel like you really understand what’s going on.

Getting help from built-in R help: The console is also your gateway to the built-in help functionality. Almost all R functions (see Workshop 2 for what “functions” are) have help files built in that will provide you with useful information about what those functions do and how to use them. You find this by typing help(function) (or ?function), where you would replace the word “function” with the name of the function you actually want to know about. It’s important to read these files closely the first time you encounter a function, but it’s also (possibly more) important to refer back to them frequently. I read once that a prominent distinction between an experienced programmer and a novice is that it takes the novice longer to look up the help for something confusing. If you have a sense for what you want to do, but don’t know or can’t remember the exact function that will do it, you can search through the help files for a term with two question marks (e.g. ??regression).

Getting help from AI and Google: Of course, the internet is also a useful resource. The AI of your choice is a bona fide expert at coding, and while it has a remarkable ability to spit out a lot of code you might not understand, it can be useful for learning R by breaking down things you don’t understand well.

R can be a little annoying to google for help with because of its name (One letter??). The go-to pre-AI source, which is still useful, is the stackoverflow website. Because this is a general-purpose resource for programming help, it will be useful to use the R tag ([R]) in your queries. A related resource is the statistics stackexchange, which is like Stack Overflow but focused more on the underlying statistical issues.

Getting help from errors: Finally, a note on errors. Novice and expert programmers alike will frequently run into errors in their R code. When this happens, processing will halt and an error message will be printed to the console. This is usually more frustrating for the novice, as the error often occurs deep within some function and the message bears no direct correspondence to what they were trying to do. A common beginner mistake is to conclude that the error message is gibberish and resign oneself to woe and dismay. It’s important to resist this urge; even if the error message is not immediately informative, it is intended to precisely convey some piece of information, and usually understanding what this information is will be the key to solving the problem.

Yes, these days, we also have the privilege of AI being able to tell us exactly what an error means. Using it can be helpful! It can also be helpful in the long run to work to understand and recognize some of the more common errors yourself so that you don’t have to rely on AI for every little thing that goes wrong.