-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_types.qmd
276 lines (181 loc) · 5.83 KB
/
data_types.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
# Data Types {#sec-data-types}
## Questions
- What are the primary data types in R?
- Can one convert between different data types?
- How does R handle different types of data?
- How can I determine variable type in R?
- What are some helpful functions for identifying and manipulating data types?
- How do data types impact my analysis and coding in R?
## Learning Objectives
- Identify and differentiate between common data types in R.
- Learn how to convert between different data types.
- Use functions like `class()`, `typeof()`, and `str()` to inspect types.
- Convert between data types using the `as.___()` functions.
## Lesson Content
### Introduction
R and RStudio utilize multiple data types to store different kinds of data.
The most common data types in R are listed below.
| **Data Type** | **Description** |
|----------------|--------------------------------------------------------|
| Numeric | The most common data type. The values can be numbers or decimals (all real numbers). |
| Integer | Special case of numeric data without decimals. |
| Logical | Boolean data type with only 2 values (`TRUE` or `FALSE`). |
| Complex | Specifies imaginary values in R. |
| Character | Assigns a character or string to a variable. The character variables are enclosed in single quotes ('character') while the string variables are enclosed in double quotes ("string"). |
| Factor | Special type of character variable that represents a categorical such as gender. |
| Raw | Specifies values as raw bytes. It uses built-in functions to convert between raw and character (`charToRaw()` or `rawToChar()`). |
| Dates | Specifies the date variable. Date stores a date and POSIXct stores a date and time. The output is indicated as the number of days (Date) or number of seconds (POSIXct) since 01/01/1970. |
### Examples of data types in R
1. Numeric
```{r}
89.98
```
```{r}
55
```
2. Integer
```{r}
5L
```
```{r}
5768L
```
3. Logical
```{r}
TRUE
```
```{r}
FALSE
```
4. Complex
```{r}
10 + 30i
```
```{r}
287 + 34i
```
5. Character or String
```{r}
'abc'
```
```{r}
"def"
```
```{r}
"I like learning R"
```
**NOTE: A comprehensive overview of handling characters with the `stringr` package will be provided in a subsequent series on the Tidyverse.**
6. Dates
```{r}
"2022-06-23 14:39:21 EAT"
```
```{r}
"2022-06-23"
```
7. Raw
```{r}
```
8. Factor
```{r}
```
### Inspecting various data types
Several functions exist to examine the features of the various data types. These include:
1\. `typeof()` – what is the data type of the object (low-level)?
2\. `class()` – what is the data type of the object (high-level)?
3\. `length()` – how long is the object?
4\. `attributes()` – any metadata available?
Let’s look at how these functions work with a few examples:
```{r}
a <- 45.84
b <- 858L
c <- TRUE
d <- 89 + 34i
e <- 'abc'
```
1. Examine the data type at a low-level with `typeof()`
```{r}
typeof(a)
```
```{r}
typeof(b)
```
```{r}
typeof(c)
```
```{r}
typeof(d)
```
```{r}
typeof(e)
```
2. Examine the data type at a high-level with `class()`
```{r}
class(a)
```
```{r}
class(b)
```
```{r}
class(c)
```
```{r}
class(d)
```
```{r}
class(e)
```
3. Use the `is.____()` functions to determine the data type. To test whether the variable is of a specific type, we can use the `is.____()` functions.
First, we test the variable a which is numeric.
```{r}
is.numeric(a)
```
```{r}
is.integer(a)
```
```{r}
is.logical(a)
```
```{r}
is.character(a)
```
Second, we test the variable c which is logical.
```{r}
is.numeric(c)
```
```{r}
is.integer(c)
```
```{r}
is.logical(c)
```
```{r}
is.character(c)
```
### Converting between various data types
To convert between data types, we can use the `as.___()` functions. These include: `as.Date()`, `as.numeric()`, and `as.factor()`. Additionally, other helpful functions include `factor()` which adds levels to the data and `nchar()` which provides the length of the data.
#### Implicit vs. Explicit conversion
Examples
```{r}
as.integer(a)
```
```{r}
as.logical(0)
```
```{r}
as.logical(1)
```
```{r}
nchar(e)
```
Order of precedence when converting between data types is:
Character `>` Numeric `>` Integer `>` Logical
## Exercises
- What are the basic data types in R? Give examples of each.
- How can you check the data type of a variable in R?
- Use the `class()`, `typeof()`, and `str()` functions to check the data type of objects.
- Describe the process of coercion in R.
- Provide an example of implicit and explicit coercion between data types.
- How can you coerce or convert between different data types in R?
- What does it mean for an object in R to have attributes? What attributes are commonly used?
## Summary
In this chapter, I have demonstrated the primary data types in R. Additionally, the learner has been trained on how to identify and convert between the different data types. Next, we will review vectors, which are key data structures in R/RStudio.