-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_struct_1.qmd
281 lines (179 loc) · 5.67 KB
/
data_struct_1.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Data Structures (Part I) {#sec-data-structure-1}
## Questions
- List the data structures that can be used in R?
- How do we define the various data structures?
- What operations can be performed on the different data structures?
- How do these data structures differ from one another in terms of storage and functionality?
## Learning Objectives
- Learn about data structures in R, specifically vectors, lists, and dataframes.
- Define and manipulate vectors, lists, and dataframes.
- Perform common operations on each data structure.
- Identify and differentiate between the diverse data structures offered by R.
- Choose the appropriate data structure based on the type and organization of your data.
- Understand the strengths and limitations of each structure for efficient analysis.
## Lesson Content
### Introduction
Data structures in R are the formats used to organize, process, retrieve, and store data. They help to organize stored data in a way that the data can be used more effectively. Data structures vary according to the number of dimensions and the data types (heterogeneous or homogeneous) contained.
The primary data structures are:
- Vectors
- Lists
- Dataframes
- Matrices
- Arrays
- Factors
In this lesson, we will review the 1st set of data structures: vectors, lists, and dataframes.
### Vectors
Discussed in the previous chapter on "Vectors" (@sec-vectors)
### Lists
Lists are objects/containers that hold elements of the same or different types. They can contain strings, numbers, vectors, dataframes, matrices, functions, or other lists. Lists are created with the list() function.
Examples:
a. Three element list
```{r}
list_1 <- list(10, 30, 50)
```
b. Single element list
```{r}
list_2 <- list(c(10, 30, 50))
```
c. Three element list
```{r}
list_3 <- list(1:3, c(50,40), 3:-5)
```
d. List with elements of different types
```{r}
list_4 <- list(c("a", "b", "c"), 5:-1)
```
e. List which contains a list
```{r}
list_5 <- list(c("a", "b", "c"), 5:-1, list_1)
```
f. Set names for the list elements
```{r}
names(list_5)
```
```{r}
names(list_5) <- c("character vector", "numeric vector", "list")
```
```{r}
names(list_5)
```
g. Access elements within a list
Return a list within a list
```{r}
list_5[1]
list_5[[1]]
```
Return an individual element within a list of lists
```{r}
list_5[[1]][1]
```
```{r}
list_5[["character vector"]]
```
h. Length of list
```{r}
length(list_1)
```
```{r}
length(list_5)
```
Flatten out into a single vector using unlist()
```{r}
unlist(list_5)
```
### Dataframes
A data frame is one of the most common data objects used to store tabular data in R. Tabular data has rows representing observations and columns representing variables. Dataframes contain lists of equal-length vectors. Each column holds a different type of data, but within each column, the elements must be of the same type. The most common data frame characteristics are listed below:
- Columns should have a name,
- Row names should be unique,
- Various data can be stored (such as numeric, factor, and character), and,
- The individual columns should contain the same number of data items.
**NOTE**: Tibbles are the modern versions of dataframes, that have improved printing and subsetting features. The tibble package is a main component of the tidyverse; however, a review of the tidyverse is beyond the scope of this book and will be discussed in subsequent lessons.
#### Creation of data frames
```{r}
level <- c("Low", "Mid", "High")
language <- c("R", "RStudio", "Shiny")
age <- c(25, 36, 47)
```
```{r}
df_1 <- data.frame(level, language, age)
```
Use `stringsAsFactors = FALSE` to prevent R from converting string columns to factors.
```{r}
df_2 <- data.frame(level, language, age, stringsAsFactors = FALSE)
```
#### Functions used to manipulate data frames
a. Number of rows
```{r}
nrow(df_1)
```
b. Number of columns
```{r}
ncol(df_1)
```
c. Dimensions
```{r}
dim(df_1)
```
d. Class of data frame
```{r}
class(df_1)
```
e. Column names
```{r}
colnames(df_1)
```
f. Row names
```{r}
rownames(df_1)
```
g. Top and bottom values
```{r}
head(df_1, n=2)
```
```{r}
tail(df_1, n=2)
```
h. Access columns / subset
```{r}
df_1$level
```
i. Access individual elements / subset
```{r}
df_1[3,2]
```
```{r}
df_1[2, 1:2]
```
j. Access columns with index
```{r}
df_1[, 3]
```
```{r}
df_1[, c("language")]
```
k. Access rows with index
```{r}
df_1[2, ]
```
**NOTE**: To view the internal structure of various data types described above, the learner can use the `str()` function.
Example
```{r}
str(list_3)
```
```{r}
str(df_1)
```
## Exercises
i. Explain the characteristics and use cases of lists in R.
ii. Create a list containing a numeric vector, character vector, and logical vector.
iii. How can you add elements to a list in R?
iv. Explain what a data frame is and why it is widely used in R.
v. Write code to create a data frame with columns for "Name," "Age," and "Gender" for three individuals.
vi. How can you combine two data frames horizontally in R?
vii. Discuss various methods for subsetting elements in data structures.
viii. How do you check the structure of a data frame? What function is used?
ix. How do you access a specific column of a data frame?
x. What are rownames and colnames in a data frame? How are they useful?
xi. How do you add a new column to an existing data frame?
## Summary
We have now covered half of the key data structures used in R. Knowledge of how to identify and manipulate vectors, lists, and dataframes will help the learner perform numerous computational tasks in R. Next, to complete our review of data structures, we will focus on matrices, arrays, and factors.