The Bare Necessesities
Learning R from scratch can be an onerous task. When I first started to learn programming, the only development environment was a text editor, and mice were just vermin that you really didn’t want in the computer lab. There just wasn’t that much to learn at once. Things have changed. Not only are you trying to learn the syntax of R but the idiosyncrasies of an IDE(RStudio), and all the assorted details like data structures and types.
The following verbosity may help users sort out some of the early issues they’ll encounter and help to minimize their learning curve for R. This is certainly not an exhaustive review of everything there is to know about R, more along the lines of “just enough to keep you out of the weeds”.
Hard to find - easy to fix
Most of the errors in R will involve one of the following:
Hashtags
#
Anything preceded by#
in a script is a comment and will not get executed. A comment is used to describe/explain what is going on in a particular part of a script. A#
can also be used to “turn” off lines of code. I’ve spent quite a lot of time wondering about why a line of code didn’t execute when I’d commented it out earlier and forgot.In RStudio, selecting several lines and then CTRL+SHIFT+C can be used to comment/uncomment entire blocks of code/text.
Arrows
<-
R has some operators that take some getting used to and who owe their origins to days before keyboards had all the keys they do now. The “left arrow” symbol represents the “assignment” operator.
If you want to create an object from some line of code, use the <- symbol(Alt <+-) to create it.Within RStudio you can use (Alt+-) as a shortcut.
Once you start searching for solutions on StackOverFlow you’ll occasionally see some “heretics” that use the equals sign in a similar fashion. This is frowned upon as equals is used to set arguments and this use can cause confusion - especially to someone who didn’t write the code. One other source of confusion is “equals doesn’t mean equals”. Two equal signs == are used for evaluation. For example -
MyVariable = 5
will assign the value “5” to MyVariable. WritingMyVariable == 5
will evaluate whether MyVariable is equal to 5 and will return a logical result - either true or false. If you use = for an evaluation RStudio will actually throw an error and ask if you really wanted == instead.Commas
,
Commas matter - they are used to separate arguments of a function and items in lists.
“I’ve given up drinking until New Years.” vs “I’ve given up, drinking until New Years.” Many of your errors will involve a missing comma.Parentheses
()
Parentheses are used to surround arguments to functions - if it’s not a missing comma, it’s probably a missing parenthesis - or has an extra one. The hardest things to find are missing parentheses or commas.The Rainbow Parentheses setting in RStudio is way more useful than it would seem.
Brackets
[]
Often referred to as “indexing”, brackets are used to reference an item by “position” within a data structure when names aren’t practical. When you get into more complex lists - like lists of lists, bracketing is a necessity. How do you refer to the nth column of a data frame in a list of data frames? - with some form of bracketing. When you run into this, it is worthwhile to stop and sort out how they work. If you view an object in the source pane of RStudio that is a list of other objects - lists, dataframes, whatever, there is a button for each row that will write the appropriate indexing for that object in the console for you. You may be the second person to learn this - you’re welcome!
If you have an error involving these symbols, Rstudio will warn you with a red dot in the left column and a squiggly red line in the vicinity of the error. The real culprit won’t always be where RStudio says though - sometimes it is farther upstream in the code.