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 datalibrary(tidyverse)# For reading in Excel fileslibrary(readxl)# For descriptive statisticslibrary(psych)# For writing data to Excellibrary(writexl)
Vectors
In Chapter 6 you also learned about calling functions on data of different modes, and assigning data to objects:
my_number<-8.2sqrt(my_number)#> [1] 2.863564my_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:
Figure 7-1. Operating on ranges 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access