5 Getting staR
ted in R
Chapter overview
Now that you have installed and tested R
and RStudio, in this chapter, you will learn how to:
- Use the
R
Console. - Do basic mathematical operations in
R
. - Create and use
R
objects. - Write and save
.R
scripts. - Add comments to your scripts.
- Keep your cool when errors pop up! 😎
If you are already familiar with the basics of R
and are keen to learn more about doing statistics in R
, you can skip most of this chapter. That said, it’s probably not a bad idea to have a go at the quiz questions and the final task to refresh your memory.
5.1 Using the Console
plot(1:10)
).One way to write R
code in RStudio is to use the Console. If you set up RStudio as recommended here, the Console should be in your top-right pane. You can type a line of code immediately after the command prompt >
and press “Enter”.
Data input is the most basic operation in R
. Try inputting a number by typing it out in the Console and then pressing “Enter”. R
will interpret the number and return it. You can input both integers (whole numbers, e.g., 13
) and decimal numbers (e.g., 0.5
).
R
can handle not only numbers but also text data, known as “character strings” or just “strings”. Strings must always be enclosed in quotation marks. You can choose to use either double quotation marks " "
or single quotation marks ' '
, but it is important to be consistent. In this textbook, we will use double quotation marks throughout.
Try first inputting a single word and then an entire sentence in the Console.
Q5.1 What happens if you enter a word without quotation marks?
5.2 Doing maths in R
R
can also be used as a very powerful calculator. The lines of code in Figure 5.3 demonstrate mathematical operations involving addition (+
), subtraction (-
), division (/
), and multiplication (*
). Try out a few yourself!
Q5.2 Try entering 13^2
in the Console. What does the ^
(caret) operator do?
Q5.3 Compare 13*13
with 13 * 13
. What is the difference in the output?
5.3 Working with R
objects
So far, we have used the Console like a calculator. It’s important to understand that, just like with a standard calculator, the output of all of our operations was not saved anywhere. If we want to store values, sequences of values, and the results of computations for later use, R
allows us to store these as “R
objects”.
5.3.1 Creating objects
We use the assignment operator (<-
) to assign a value or sequence of values to an object name.
Write out the following line to create an object called my.favourite.number
that contains your own favourite number.
<- 13 my.favourite.number
When you enter this line in the Console and press “Enter”, it should look like nothing happened: R
does not return anything in the Console. Instead, it saves the output in an object called my.favourite.number
. However, if you look in your Environment pane, you should see that an object has appeared (Figure 5.4).
To save an object containing a character string, we use quotation marks. Create an object called my.favourite.word
containing your favourite word (in any written language of your choice).
<- "empathy" my.favourite.word
Your Environment pane should now contain two objects. You can print the content of a stored object by entering the object name in the Console and then pressing “Enter” (see Figure 5.5).
↹
or ⇥
). RStudio will then give you a drop-down menu with possible options. Select the one you want by clicking on it or pressing “Enter”.5.3.2 Object types
These two objects are of different types. We can use the class()
function to find out which type of object an object is.
Here, my.favourite.number
is a numeric object, while my.favourite.word
is a character object.
5.3.3 Naming objects
Object naming conventions in R
are fairly flexible. We can use dots (.
), underscores (_
) and capital letters to make our object names maximally informative and easy for us humans to read. However, spaces and other symbols are not allowed. All of these options work:
<- "cheerful"
word2 <- "cheerful"
my.second.word <- "cheerful"
my_second_word <- "cheerful" MySecondWord
Q5.4 Which of these object names are not allowed in R
? Try to create an object with each of these names and see if you get an error message or not.
😇 Hover for a hint
Object names should not contain spaces or symbols like !
, nor should they contain hyphens as the hyphen is reserved for the mathematical operator “minus”. Digits can be used anywhere except at the beginning of an object name. And whilst it is possible to have special characters such as accented letters like “è”, it is not recommended that you use them for object names.
5.3.4 Overwriting and deleting objects
Object names are unique. If you create a new object with an existing object name, it will overwrite the existing object with the new one. In other words, you will lose the values that you saved in the original object. Try it out by running this line and observing what happens in your Environment pane:
<- "surprised" word2
Earlier on, you created an object called word2
which contained the string “cheerful”. But, by running this new line of code, “cheerful” has been replaced by the string “surprised” - with no warning that you were about to permanently delete “cheerful”! 😲
The command to delete a single object from your environment is remove()
or rm()
. Hence, to permanently delete the object MySecondWord
, you can use either of these commands:
remove(MySecondWord)
rm(MySecondWord)
5.4 Working with .R
scripts
If we shut down RStudio right now, we will lose all of our work so far. This is because the objects that we have created are only saved in the environment of our current R
session. Whilst this might sound reckless, it is actually a good thing: In Section 4.3.1 we set our ‘Global Options’ settings in RStudio such that, whenever we restart RStudio, we begin with a clean slate, or a perfectly clean and tidy kitchen. We don’t want any dirty dishes or stale ingredients lying around when we enter the kitchen! With this in mind, close RStudio now and open it again to start a new R
session.
You should now have an empty history in your Console pane and an empty Environment pane. Whilst nobody wants to start cooking in a messy kitchen, it’s also true that, if we want to remember what we did in a previous cooking/baking session, we should write it down. The pages of our recipe book are .R
scripts. In the following, we will see that writing scripts is much better than running everything from the Console. It allows us to save and rerun our entire analysis pipeline any time we want. It also ensures that our analyses are reproducible and saves us time as we don’t have to rewrite our code every time. Crucially, if we made a mistake at any stage, we can go back and correct it and rerun the entire corrected script at the click of a button.
5.4.1 Creating a new .R
script
There are three ways to create a new .R
script in RStudio. Pick the one that you like best:
- Navigate to the top menu item “File”, then select “New File”, then click on “R Script”.
- Click on the icon with a white page and a green plus button in the top left corner of the tool bar.
- Use the keyboard shortcut
Shift + Ctrl/Cmd + N
.
Whichever option you chose, RStudio should have opened an empty file in a fourth pane (see Figure 5.8). This is the “Source pane” and it should have appeared in the top-left corner of your RStudio window.
5.4.2 Running code from an .R
script
We can now type our code in this empty .R
script in the Source pane, just like we did in the Console. Type the following lines of code in the script (see Figure 5.9):
13*13
<- 13
my.favourite.number <- "empathy" my.favourite.word
You will have noticed that when you pressed “Enter” after every line, nothing happened: Nowhere can we see the result of 13*13
, nor have our two objects been saved to the environment as the Environment pane remains empty (see Figure 5.9). Just like a recipe for a cake is not an actual, delicious cake, but simply a set of instructions, a script is only a text file that contains lines of code as instructions. For these instructions to be executed, we need to send them to the R
Console where they will be interpreted as R
code.
To send a line of code to the Console (also referred to as “running” a line of code), select the line that you want to excecute, or place your mouse cursor anywhere within that line and then click on the “Run” button (in the top-right corner of the pane, see Figure 5.8) or use the keyboard shortcut Ctrl/Cmd + Enter
.
Try out these two options to run the three lines of code of your script and check that a) you are seeing the result of the mathematical operation in the Console output and b) two objects have been added to your environment.
5.4.3 Saving an .R
script
It is now very easy to rerun this script any time we want to redo this calculation and recreate these two R
objects. However, our .R
script is not yet saved! RStudio is warning us about this by highlighting the file name “Untitled1*” in red (see Figure 5.8). Just like with any unsaved computer file, if we were to shut RStudio down now, we would lose our work. So, let us save this .R
script locally, that is on our own computer. To do so either:
- Navigate to the top menu item “File” and then click on “Save”,
- Click on the save icon 💾, or
- Use the keyboard shortcut
Ctrl/Cmd+ S
.
Give your script a meaningful file name. Remember that file names should be both computer-readable and human-readable. If you navigate to the folder where you saved your .R
script, you should see that its file extension is .R
. You should also see that it is a tiny file because it contains nothing more than a few lines of text. If you double click on an .R
file, RStudio should automatically open it. However, if you wanted, you could open .R
files with any text-processing software, such as LibreOffice Writer or Microsoft Word.
5.4.4 Writing comments in scripts
Just like in a recipe book, in addition to writing the actual instructions, we can also write some notes, for example to remind ourselves of why we did things in a particular way or for what occasion we created a special dish. In programming, notes are called “comments” and they are typically preceded by the #
symbol.
Thus, if a line starts with a #
symbol, we say that it is “commented out”. RStudio helpfully displays lines that are commented out in a different colour. These lines will not be interpreted as code even if you send them to the Console. Write the following lines in your script and then try to run them.
#13^13
#StringObject3 <- "This line has been commented out so the object will not be saved in the environment even if you try to run it."
As you can see, nothing happens. You can also add comments next to a line of interpretable code. In this case, the code is interpreted up until the #
. This can be helpful to make a note of what a line of code does, e.g.:
sqrt(169) # Here the sqrt() function will compute the square root of 169.
[1] 13
It is good practice to comment your code when working in an .R
script. Comments are crucial for other people to understand what your code does and how it achieves that. But even if you are confident that you are the only person who will ever use your code, it is still a very good idea to use comments to make notes documenting your intentions and your reasoning as you write your script.
Finally, writing comments in your code as you work through the examples in this book is a great way to reinforce what you are learning. From this chapter onwards, I recommend that, for each chapter, you create an .R
script documenting what you have learnt, adding lots of comments to help you remember how things work. This is generally more efficient (and less error-prone!) than trying to take notes in a separate document (e.g., in a Microsoft Word file) or on paper.
5.5 Using relational operators
Now that we have saved some objects in our environment, we can use them in calculations. Try out the following operations (and any other that take your fancy) with your own favourite number:
/ 2 my.favourite.number
[1] 6.5
* my.favourite.number my.favourite.number
[1] 169
In additional to the mathematical operations that we saw in Section 5.2, we can also use relational operators such >
, <
, <=
, >=
, ==
and !=
to make all kinds of comparisons. Try out the following commands to understand how these relational operators work and then have a go at the quiz questions.
> 10
my.favourite.number < 10
my.favourite.number == 25
my.favourite.number >= 13
my.favourite.number <= -13
my.favourite.number != 25 my.favourite.number
Q5.5 What is the relational operator that checks whether a value is “more than or equal to” another value?
Q5.6 What is the relational operator that checks whether a value “is not equal to” another value?
The relational operators ==
and !=
can also be used with character objects. Find out how they work by first creating a new character object with a word that was added to the 2025 edition of the popular French dictionary Petit Larousse:
<- "écogeste" New.French.Word
Then copy these lines of code to test how these relational operators work with string characters.
== "écogeste"
New.French.Word != "trottinettiste" New.French.Word
You will have noticed that the relational operator ==
tests whether two strings are the same and returns TRUE
if that’s the case. In contrast, !=
tests whether two strings are different and will therefore return FALSE
if they are not different.
Above, we created the following R
character string object:
<- "écogeste" New.French.Word
Q5.7 Why does this line of code return FALSE
even though New.French.Word
was assigned the character string “écogeste”?
== "ecogeste" New.French.Word
Q5.8 Why does this line of code return FALSE
?
== " écogeste" New.French.Word
Q5.9 Why does this line of code return FALSE
?
== "Écogeste" New.French.Word
Q5.10 Why does this line of code return FALSE
?
!= "écogeste" New.French.Word
5.6 Dealing with errors 🤬
If you try to run code that R
cannot interpret, your Console will display an error message in red. A large part of learning to code is really about learning how to interpret these error messages, and making the most common errors often enough that you immediately know how to fix them. The process of fixing programming errors is called debugging and often involves an array of emotions (see Figure 5.10).
In R
, you will regularly encounter one particular problem that we will call the “plus-situation”. Let’s take a closer look at this error. Copy and paste this exact line of code in your R
Console and hit “Enter” to run it:
sqrt(my.favourite.number
Notice that, in this erroneous line of code, we have (intentionally) forgotten to include the final bracket. As a result, after you hit “Enter”, the Console output shows a “+
” instead of the result of the mathematical operation (see Figure 5.11). The “+
” indicates that the line is incomplete and therefore cannot be interpreted yet. Whenever you see a “+
” at the start of a command in the Console, R
is asking you to complete your line of code.
There are two ways to fix this. The first method is to complete the line of code directly in the Console. In the above case, this means adding the closing bracket “)
” after the “+
” and hitting “Enter” again. Now that the line has been completed, R
is able to interpret it as a valid R
command and therefore outputs the expected result.
If you are running a line of code just once, from the Console, this first method is fine. As we have seen above, however, most of the time, you will write your code in a script rather than in the Console. So this first, on-the-fly method is only recommendable for lines of code that you will genuinely only need once. These include commands to install packages, like install.packages("janeaustenr")
, or to consult documentation files, e.g., help(janeaustenr)
.
Given that we will mostly be working in scripts to ensure that our analyses are reproducible (see Chapter 13), let’s now generate this error from an .R
script. To do so, copy and paste the erroneous line of code in your .R
script and try to run it by either clicking on the “Run” icon or using the shortcut Ctrl/Cmd + Enter
.
sqrt(my.favourite.number
Again, our incomplete line of code cannot be interpreted and this generates a “plus-situation” appears in the Console. Now, correct the error in your script by adding the missing closing bracket and try to run the command again.
sqrt(my.favourite.number)
As shown in Figure 5.12, even though we have corrected the problem, we now get an error! 🤯 At first sight, this does not make sense, but look carefully at what happened in the Console: The line of code that R
tried to interpret (in blue) is sqrt(my.favourite.number + sqrt(my.favourite.number)
, i.e., the combination of the incomplete version of the command plus the complete one. This is obviously nonsense and R
tells us so by outputting an error message (see Figure 5.12)!
To run a new line of code, we must see the command prompt >
in the Console. So, let’s generate the error again and learn how to fix it with the second method. Add this erroneous line to your script again and run it:
sqrt(my.favourite.number
The plus-situation arises again, but we will now solve it using the second method. Head over to the Console and place your cursor next to the +
. This time, instead of completing the line by adding a closing bracket, press the Escape key (“Esc”) on your keyboard. This will cancel the incomplete line of code. Then, you can add the missing )
in your script and rerun the newly completed line of code from the Source pane.
This second method is the one you should use when you are documenting your code in a script. If you don’t make the changes immediately in your script, you will forget and you will run into this error again in the future. Think of it like a pastry chef who realises that they need to put a little more baking powder in a cake batter for the texture to be just right, but does not make a note of that change in their recipe book. It’s quite likely that the chef will forget the next time they bake the cake. If it is one of their assistants who prepares the batter, they will will have no way of knowing that the chef made that change!
Learning to make sense of error messages is a very important skill that, like all skills, takes practice. Most errors are very easy to fix, if you keep your cool. In fact, 90% of errors are simply typos1, so really nothing worth stressing about!
Copy and paste the following lines of code in a new .R
script. Try to run each line individually. Each line will generate an error of some kind. In some cases, RStudio will warn you in advance that a line of code is likely wrong by displaying a red cross icon to the left of the erroneous line. If you hover over the red cross icon, RStudio will display a message that may help you to fix the error.
Can you decode the error messages to find out what is causing these errors and fix these ten erroneous commands?
<- "empathy"
my.favourite.word <- 13
my.favourite.number
# Error 1:
+ my.favorite.number
my.favourite.number
# Error 2:
-Fav-Word <- "Ach so!"
Negin
# Error 3:
^2
my.favourite.numbers
# Error 4:
<- 52
ömers_favourite_ number
# Error 5:
= my.favourite..number
ömers_favorite_number
# Error 6:
*2 -> half.my.fav.number
my.favourite.number
# Error 7:
's.favourite.number <- 5
rose
# Error 8:
BestWordEver <- "supercalifragilisticexpialidocious
# Error 9:
2FavNumbers <- my.favourite.number + ömers_favourite_number
# Error 10:
good.luck <- موفق باشيد"
Debugging is an unavoidable part of writing code. If you’re stuck and starting to feel frustrated, the best thing you can usually do is to take a short break!
The first error was
object 'my.favorite.number' not found
. This means that the objectmy.favorite.number
is not stored in your environment. If you think it is, the problem is most likely due to a typo. Here,my.favorite.number
uses American English spelling, whereas we used British English spelling (with a “u”) when we created the object. To correct the error, you need to use exactly the same spelling as when you created the object.The second error is also
object 'Negin' not found
. However, here we do not expect an object calledNegin
to be in the environment because what we are actually trying to do is create and save a new object calledNegin-Fav-Word
! The problem is thatR
interprets the hyphens in this object name as “minus” and therefore tries to find the objectNegin
in order to then subtractFav
andWord
from it. To correct this error, you need to remove the hyphens or replace them by dots.The third error is yet another
object not found error
. It is another typo: the correct object name is not in the plural form.The fourth error is
Error: unexpected symbol in "ömers_favourite_ number"
. In addition, RStudio warned us that there were some “unexpected tokens” in this line of code. The unexpected item is the space between_
andnumber
. To fix this error, you need to remove this space character.The object
my.favourite..number
is not found because the name of the object saved in the environment does not have two consecutive dots. Note that the error does not come from the fact that this line begins with some white space and includes multiple space characters after the=
sign. These added spaces make the line more difficult for us humans to read, butR
simply ignores them. Hence, to fix this error, what you need to do is remove one of the consecutive dots in the object name.It is also worth noting that, once you’ve removed the extra dot, this line of code replaces the value originally stored in
ömers_favourite_number
with the value stored inmy.favourite.number
. If you check your environment pane, you will see that the command has changedömers_favourite_number
to13
with no warning!! In other words, here, the equal sign=
behaves in the same way as the assignment operator<-
.If you tried to run this line, you will have noticed that it does not actually generate an error. However, you may have noticed that the assignment operator is in the opposite direction (
->
instead of<-
). This means thatmy.favourite.number
times two is assigned to a new object calledhalf.my.fav.number
. Having observed this, you will hopefully want to either amend the line for it to make mathematical sense (my.favourite.number/2 -> half.my.fav.number
) or change the name of the new object for it to be meaningful (my.favourite.number*2 -> twice.my.fav.number
).Running this line will have caused you to run into a
+
situation in the console. As explained in Section 5.6, to get out of it, first take your mouse cursor to the Console pane and press the Escape key on your keyboard to cancel the erroneous line. Whilst there is no error message to help you understand where the problem is coming from, you should see that RStudio helpfully displays a red cross icon to the left of the line; hovering over it displays a multi-line message. The first line is the relevant one:unexpected token 's.favourite.number <- 5
. This tells us that apostrophes are forbidden in object names. Remove the'
and the error will be fixed.This line also causes a
+
situation. In this case, it is due to a missing quotation mark. To fix this error, first cancel the incomplete line of code by escaping it. Then, add the missing double quotation mark in your script and rerun the completed line.The message
Error: unexpected symbol in "2FavNumbers"
is due to the fact that object names cannot start with a number. Change the object name to something likeTwoFavNumbers
orFav2Numbers
to fix this error.Here, too, the error message reads:
unexpected symbol
. However, it is important to remember that the unexpected symbol is not within the character string, but rather has to do with the code syntax used to assign the Persian string togood.luck
. In other words, the problem has nothing to do with the fact that the string is in Persian, but rather that one of the quotation marks is missing. You can fix the error by ensuring that the phrase is enclosed in quotation marks.
Check your progress 🌟
You have successfully completed 0 out of 10 questions in this chapter.
Are you confident that you can…?
If that’s the case, you’re ready to go through Chapter 6 to learn how to import research data into R
so that, in the following chapters, you can analyse real-life linguistics data!
I must confess that I made this number up. I don’t have any reliable number, but it’s fair to say that it’s a very large proportion!↩︎