Chapter 2 Some R basics
In this Chapter we’ll introduce you to using R and RStudio to perform some basic R tasks such as creating objects and assigning values to objects, exploring different types of objects and how to perform some common operations on objects. We’ll also learn how to get help in R and highlight some resources to help support your R learning. Finally, we’ll cover how to save your work.
Before we continue, here are a few things to bear in mind as you work through this Chapter:
R is case sensitive i.e.
Ais not the same as
anovais not the same as
Anything that follows a
#symbol is interpreted as a comment and ignored by R. Comments should be used liberally throughout your code for both your own information and also to help your collaborators. Writing comments is a bit of an art and something that you will become more adept at as your experience grows.
In R, commands are generally separated by a new line. You can also use a semicolon
;to separate your commands but this is rarely used.
If a continuation prompt
+appears in the console after you execute your code this means that you haven’t completed your code correctly. This often happens if you forget to close a bracket and is especially common when nested brackets are used (
(((some command))). Just finish the command on the new line and fix the typo or hit escape on your keyboard (see point below) and fix.
In general, R is fairly tolerant of extra spaces inserted into your code, in fact using spaces is actively encouraged. However, spaces should not be inserted into operators i.e.
<-should not read
< -(note the space). See Google’s style guide for advice on where to place spaces to make your code more readable.
If your console ‘hangs’ and becomes unresponsive after running a command you can often get yourself out of trouble by pressing the escape key (esc) on your keyboard or clicking on the stop icon in the top right of your console. This will terminate most current operations.