R 4 Beginners Chapter 2 - Coding Basics
R 4 Beginners
An exploration of data science as taught in R for Data Science by Hadley Wickham and Garrett Grolemund. This blog is meant to be a helpful addition to your own journey through the book. No prior programming experience necessary!
Chapter 2: Coding Basics
Before we get into what kinds of cool things we can do with R, there are a couple of things to know that will be useful. R4DS has this section after the Data Visualization chapter… but I think it’s more helpful to get these details out of the way early. It may not be as satisfying or fun, but it’ll save us frustration down the road! There are two concepts we’ll go through this week: creating objects and calling functions.
Let’s create a very simple object in R:
x <- 9
If we were to read this statement out loud, it would be “the object ‘x’ gets value 9”. What we’re actually doing in R is making an assignment statement and “assigning” the value of 9 to the object
x. Now, if you run this code in RStudio, you’ll notice that nothing seems to happen… But stuff is happening behind the scenes! Try running this:
x <- "hello world" x
RStudio will print out the value of the object (in this case the words “hello world”). If you want skip the second step, you can surround the whole thing with parentheses:
(x <- "hello world")
As you can see from the examples above, there are different kinds of objects in R that can take different kinds of values:
- Integers (like 2 or 13)
- Doubles (short for “double precision floating point numbers”) that take decimals values, like 3.4 or 12.65
- Characters, which represent string values. Note that for strings, you’ll need to put the value in quotation marks like we did above with “hello world”. We commonly think of strings as letters or words, but anything put in quotation marks will be a string.
These are not the only kinds of objects you’ll see in R, but they’re very common.
The names for our objects also don’t have to be single letters! They do have to start with a letter, and can’t contain any spaces. They can only contain letters, numbers,
.. As we go through this book, and as you (hopefully!) code more in the future, you’ll find there are some conventions that people like. It honestly doesn’t really matter how you name your objects at the moment, but for your own sake it helps to at least be internally consistent.
Something to note is that while you technically can use
= rather than
<-, at least for some things, don’t. They are subtly different operations, and it could make trouble for you later. May as well get into the habit of doing it right early on. And on that note, RStudio has a nifty keyboard shortcut for
<-: hold Alt (or option on a Mac) and press the - (minus sign) key.
Finally, if you look in the top right corner of the RStudio window you’ll see all of your objects listed under the “Environment” tab.
One last thing. What we’re referring to as “objects” here are often called “variables” in other programming languages. Because the word “variable” means something specific in statistics, and R is a statistical tool, this is important to keep in mind. I will try to be unambiguous as we go forward (though I hope you’ll forgive me if I slip up). If you’d like to know more about objects in R, check out this page.
We’ve actually already called a couple of functions! Remember
library() from Chapter 1? Those are examples of functions. They are ways that we tell R we want to do something specific to an argument (the bit that goes in the parentheses). For
install.packages() the argument was the string “tidyverse”, while for
library() it is the group of packages called
tidyverse- this explains why we need the quotation marks for
install.packages() but not for
Another example of a built-in function in R is
seq(). Try running this in RStudio:
The output is a sequence of numbers starting with 1 and ending with 10 (so you can guess what our two arguments are here). In fact,
seq() has five possible arguments, and depending on which ones you specify your sequence can look quite different. If you’re curious about
seq() (or really, anything you want to know more about in R), try running something like this:
Your help tab in the bottom right corner will give you some useful info.
Now that we have some of the basics out of the way, next we’ll dive into data visualization!