Chapter 7. Data Structures in R
Toward the end of Chapter 6 you learned how to work with packages in R. It’s common to load any necessary packages at the beginning of a script so that there are no surprises about required downloads later on. In that spirit, we’ll call in any packages needed for this chapter now. You may need to install some of these; if you need a refresher on doing that, look back to Chapter 6. I’ll further explain these packages as we get to them.
# For importing and exploring data
library
(
tidyverse
)
# For reading in Excel files
library
(
readxl
)
# For descriptive statistics
library
(
psych
)
# For writing data to Excel
library
(
writexl
)
Vectors
In Chapter 6 you also learned about calling functions on data of different modes, and assigning data to objects:
my_number
<-
8.2
sqrt
(
my_number
)
#> [1] 2.863564
my_char
<-
'Hello, world'
toupper
(
my_char
)
#> [1] "HELLO, WORLD"
Chances are, you generally work with more than one piece of data at a time, so assigning each to its own object probably doesn’t sound too useful. In Excel, you can place data into contiguous cells, called a range, and easily operate on that data. Figure 7-1 depicts some simple examples of operating on ranges of both numbers and text in Excel:
Earlier I likened the mode of an object to a particular type of shoe in a shoebox. The structure of an object is the ...
Get Advancing into Analytics 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.