Welcome to the first lesson of “Working in R.” The purpose of these short vignettes is not to be comprehensive, but to provide a foundation to get you on your feet as you explore data analysis in R. Each lesson begins with an overview, which lists the sort of things that will be covered in the lesson. For example, this lesson covers the very basics of using R: how to use the console and how to create objects. It concludes with a quick review of error messages in R. I hope this is useful, and please send any corrections my way.
All operations in R are done in the console. And we can type our commands directly into the console. It turns out it is more convenient for replication to write our commands out in a text file. R Studio provides a text editor that allows you to create and run your own code by copying the commands in your text file into the console.
It is important to note, however, that you can type everything directly into the console. This can be done for simple things, such as installing packages (more on this later), or when you want to perform back-of-the-hand calculations. For instance, R can be used as a giant calculator. For example, we could ask it to perform the following addition: 2 + 3 is 5.
2 + 3
## [1] 5
That would be silly of course, but in a pinch, it’s important to know that there’s always R!
Key to understanding R is that it is an object-based environment. This means that you navigate R by assigning values to objects and then performing operations on these objects. For example, we can perform the same addition from before by assigning the values 2 and 3 to R objects as below. To assign a value we use “<-” (read as gets).
a<-2 # a gets 2
b<-3 # b gets 3
# And so when we call
a #the console returns 2
b #the console returns 3
a + b #returns the operation 2+3 which is equal to 5
#the order in which you place the operations matters so that a-b and b-a is not the same
a-b
b-a
Objects can take almost any value. For example, objects can store results, and they can also store other objects. Be careful though, cause objects will not auto-update when you change any objects they contain, and they will be easily be renamed. Note also that we cannot assign numerical objects like 2, another number.
z <- a+b #z gets the addition of a + b
a <-9 # I've now changed the value of a, but z does not know this so
z # returns 5 instead of 12, until I rerun the object z
z <- a+b
z # now 12
R can only do its best. That is, it will only do what it is literally told, which is not always what you want it to do. The lesson is that you should look over your code carefully to ensure that R is doing what you want it to do. It also means that on occasion you will mess up. Struggling with a new coding language is key to learning how to use. Sometimes, however, R will return an error message. For instance, say we want to perform the operation 12-c, but we have not defined c. Try it!
12+ c
#Error in 12 + c : non-numeric argument to binary operator
The error message that will show suggests that it cannot perform the operation because there is a non-numeric argument. We can tell it must be c, and upon further reflection we notice that we have not defined it so to solve this issue, we must first define that object.
That is it for today! In the next lesson we will learn how to deal with groupings of numbers, the foundations of datasets.