Since about October last year, I’ve been writing an introduction to R book. It’s
been quite the experience. I’ve finally started making time to document some of
the interesting things I’ve learned (about R, about writing, about how to
bring those two together) along the way.
The book is aimed at proper beginners; people with absolutely no formal coding
experience. This tends to mean people coming from Excel who need to do more than
a spreadsheet can/should.
I’m writing an R book for real beginners (ppl with 0 code XP) via @ManningBooks! What tripped you up most when you first learned R? Pls RT!
— Jonathan Carroll (@carroll_jono) September 27, 2016
Most of the books I’ve looked at which claim to teach programming begin with
some strong assumptions about the reader already knowing how to program, and
teach the specific syntax of some language. That’s no good if this is your first
language, so I’m working towards teaching the concepts, the language, and the
syntax (warts and all).
The book is currently available under the Manning Early Access Program (MEAP)
which means if you buy it you get the draft of the first three chapters right
now. If you find something you still don’t understand, or you don’t like how
I’ve written some/all of it, then jump onto the forum and let me know. I make
more edits and write more chapters, and you get updated versions. Lather, rinse,
repeat until the final version and you get a polished book which (if I’m any
good) contains what you want it to.
I’m genuinely interested in getting this right; I want to help people learn R. I
contribute a bit of time on Stack Overflow answering people’s questions, and
it’s very common to see questions that shouldn’t need asking. I don’t blame the
user for not knowing something (a different answer for not searching, perhaps),
but I can help make the resource they need.
Chapter 1 is a free download, so please check that out too! At the moment the
MEAP covers the first three chapters, but the following four aren’t too far
I’ll document some of the behind-the-scenes process shortly, but for now here’s
an excerpt from chapter 2:
In order to do something with our data, we will need to tell
R what to call
it, so that we can refer to it in our code. In programming in general, we
typically have variables (things that may vary) and values (our data). We’ve
already seen that different data values can have different types, but we
R to store any of them yet. Next, we’ll create some variables
to store our data values.
If we have the values
8 and we want to do something with them, we can
use the values literally (say, add them together as
4 + 8). You may be
familiar with this if you frequently use Excel; data values are stored in cells
(groups of which you can opt to name) and you tell the program which values you
wish to combine in some calculation by selecting the cells with the mouse or
keyboard. Alternatively, you can opt to refer to cells by their grid reference
A1). Similarly to this second method, we can store values in variables
(things that may vary) and abstract away the values. In
R, assigning of
values to variables takes the following form
The assignment operator
can be thought of as storing the value/thing on
the right hand side into the name/thing on the left hand side. For example, try
x into the
Console then press Enter:
xhas been assigned the value
You could just as easily use the equals sign to achieve this;
x = 4 but I
recommend you use
for this for reasons that will become clear later.
You’ll notice that the
Environment tab of the
Workspace pane now lists
Values and shows the number 4 next to it, as shown in [fig-x_eq_4]
xhas been assigned the value
What happened behind the scenes was that when we pressed Enter,
the entire expression that we entered (
x ) and evaluated it. Since we
R to assign the value
4 to the variable
R converted the value
4 to binary and placed that in the computer’s memory.
R then gives us a
reference to that place in the computer’s memory and labels it
x. A diagram of
this process is shown in [fig-assign] Nothing else appeared in the
Console because the action of assigning a value doesn’t return anything
(we’ll cover this more in our section on functions).
This is overly simplified, of course. Technically speaking, in
R, names have
objects rather than the other way around. This means that
R can be quite
memory efficient since it doesn’t create a copy of anything it doesn’t need to.
On “hidden” variables
Variables which begin with a period (e.g.
We can retrieve the value assigned to the variable
x by asking
R to print
the value of
print(x = x)
#>  4
for which we have a useful shortcut — if your entire expression is just a
R will assume you mean to
print() it, so
#>  4
works just the same.
Now, about that
: it’s important to know that in
R, there’s no such thing
as a single value; every value is actually a vector of values (we’ll cover
these properly in the next chapter, but think of these as collections of values
of the same type).
R prints a value it allows for the case where the value contains more
than one number. To make this easier on the eye, it labels the first value
appearing on the line by it’s position in the collection. For collections
(vectors) with just a single value, this might appear strange, but this makes
more sense once our variables contain more values
# Print the column names of the mtcars dataset names(x = mtcars)
#>  "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" #>  "carb"
We can assign another variable to another value
There are few restrictions for what we can name our data values, but
complain if you try to break them. Variables should start with a letter, not a
number. Trying to create the variable
2b will generate an error. Variables can
also start with a dot (
.) as long as it’s not immediately followed by a
number, although you may wish to avoid doing so. The rest of the variable name
can consist of letters (upper and lower case) and numbers, but not punctuation
_) or other symbols (except the dot, though again, preferably
There are also certain reserved words that you can’t name variables as. Some
are reserved for built-in functions or keywords
Others are reserved for particular values
We’ll come back to what each of these means, but for now you just need to know
that you can’t create a variable with one of those names.
On overwriting names
What you can do however, which you may wish to take care with, is overwrite
If you were translating an equation into code, and wanted to enter the value
The default value can still be accessed by specifying the package in which it is
#>  3.141593
This is also an issue for functions, with the same solution; specify the package
We’ll cover how to do things to our variables in more detail in the next
section, but for now let’s see what happens if we add our variables
in the same way as we did for our regular numbers
x + y
#>  12
which is what we got when we added these numbers explicitly. Note that since our
expression produces just a number (no assignment), the value is printed. We’ll
cover how to add and subtract values in more depth in our section on basic mathematics.
R has no problems with overwriting these values, and it doesn’t mind what data
you overwrite these with.
#>  "banana"
R is case-sensitive, which means that it treats
A as distinct
names. You can have a variable named
myVariable and another named
and another named
R will hold the value assigned to each
There are only two hard things in Computer Science: cache invalidation and naming things.
Principal Curmudgeon Netscape Communications Corporation
I said earlier that
R won’t keep track of your units so it’s a good idea to
name your variables in a way that makes logical sense, is meaningful, and will
help you remember what it represents. Variables
y are fine for playing
around with values, but aren’t particularly meaningful if your data represents
speeds, where you may want to use something like
speed_kmph for speeds in
kilometers per hour. Underscores (
_) are allowed in variable names, but
whether or not you use them is up to you. Some programmers prefer to name
variables in this way (sometimes referred to as ‘snake_case’), others prefer
‘CamelCase’. The use of periods (dots,
.) to separate words is discouraged for
reasons beyond the scope of this book.
Be careful when naming your variables. Make them meaningful and
If you’re familiar with working with data in a spreadsheet program (such as
Excel), you may expect your variables to behave in a way that they
won’t. Automatic recalculation is a very useful feature of spreadsheet programs,
but it’s not how
If we assign our two variables, then add them, we can save that result to
This has the value we expect
print(x = sum_of_a_and_b)
#>  12
Now, if we change one of these values
this has no impact on the value of the variable we created to hold the sum
print(x = sum_of_a_and_b)
#>  12
Once the sum was calculated, and that value stored in a variable, the connection
to the original values was lost. This makes things reliable because you know
for sure what value a variable will have at any point in your calculation by
following the steps that lead to it, whereas a spreadsheet depends much more on
its current overall state.
If you’ve read some
R code already, you’ve possibly seen that both
= are used to assign values to objects, and this tends to cause some
R will accept either when assigning variables, so in
that respect it comes down to a matter of style (I still highly recommend
). The big difference comes when using functions that take
arguments — there you should only use
= to specify what the value of the
argument. For example, when we inspected the
mtcars data, we could
specify a string with which to indent the output
str(object = mtcars, indent.str = '>> ')
#> 'data.frame': 32 obs. of 11 variables: #> >> $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... #> >> $ cyl : num 6 6 4 6 8 6 8 4 4 6 ... #> >> $ disp: num 160 160 108 258 360 ... #> >> $ hp : num 110 110 93 110 175 105 245 62 95 123 ... #> >> $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ... #> >> $ wt : num 2.62 2.88 2.32 3.21 3.44 ... #> >> $ qsec: num 16.5 17 18.6 19.4 17 ... #> >> $ vs : num 0 0 1 1 0 1 0 1 1 1 ... #> >> $ am : num 1 1 1 0 0 0 0 0 0 0 ... #> >> $ gear: num 4 4 4 3 3 3 3 4 4 4 ... #> >> $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
If we had used
= for either argument, then
R would treat
that as creating a new variable
indent.str with value
'>> ' respectively, which isn’t what we want.
Assigning to variables.
#>  4.8
#> num 4.8
#>  "banana"
#> chr "banana"
Note that we didn’t need to tell
R that one of these was a number and one was
a string, it figured that out itself. It’s good practice (and easier to read) to
line up vertically when defining several variables:
but only if this can be achieved without adding too many spaces (exactly how
many is too many is up to you).
Watch this space!
An extra space can make a big difference to the syntax. Compare:
#>  FALSE
In the first case we assigned the value
Now that we know how to provide some data to
R, what if we want to explicitly
R that our data should be of a specific type, or we want to convert our
data to a different type? That’s an article for another day.
Rhas no scalar types.
Rto denote functions acting on a specific class, such as
Source:: R News