Chapter 3. R Objects
In this chapter, youâll use R to assemble a deck of 52 playing cards.
Youâll start by building simple R objects that represent playing cards and then work your way up to a full-blown table of data. In short, youâll build the equivalent of an Excel spreadsheet from scratch. When you are finished, your deck of cards will look something like this:
face suit value king spades13
queen spades12
jack spades11
ten spades10
nine spades9
eight spades8
...
Do you need to build a data set from scratch to use it in R? Not at all. You can load most data sets into R with one simple step, see Loading Data. But this exercise will teach you how R stores data, and how you can assembleâor disassembleâyour own data sets. You will also learn about the various types of objects available for you to use in R (not all R objects are the same!). Consider this exercise a rite of passage; by doing it, you will become an expert on storing data in R.
Weâll start with the very basics. The most simple type of object in R is an atomic vector. Atomic vectors are not nuclear powered, but they are very simple and they do show up everywhere. If you look closely enough, youâll see that most structures in R are built from atomic vectors.
Atomic Vectors
An atomic vector is just a simple vector of data. In fact, youâve already made an atomic vector, your die
object from Part I. You can make an atomic vector by grouping some values of data together with c
:
die<-
c(
1
,
2
,
3
,
4
,
5
,
Get Hands-On Programming with R now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.