-
Notifications
You must be signed in to change notification settings - Fork 17
/
p1c3-data-types.qmd
102 lines (70 loc) · 3.36 KB
/
p1c3-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
# Data Types
Data is stored differently depending on what it represents when programming. For example, a number is going to be stored as a different data type than a letter is.
There are five basic data types in R that each store a single value:
- **Numeric** - This is the default type for numbers, e.g. integers and doubles.
- *Double* - A double allows you to store numbers as decimals. This is the default treatment for numbers.
- *Integer* - An integer is a subset of the numeric data type. This type will only allow whole numbers and is denoted by the letter "L".
- **Complex** - This type is created by using the imaginary variable "i".
- **Character** - This type is used for storing non-numeric text data.
- **Logical** - Sometimes referred to as "boolean", this data type will store either "TRUE" or "FALSE".
- **Raw** - Used less often, this data type will store data as raw bytes.
In addition, each missing values can be specified with the special `NA` type,
which can represent each of the data types listed above.
## Numeric
### Double
Let's explore the "double" data type by assigning a number to a variable and then check its type by using the "typeof" function. Alternatively, we can use the "is.double" function to check whether or not the variable is a double.
```{r}
x <- 6.2
typeof(x)
is.double(x)
```
Next, let's check whether or not the variable is numeric by using the "is.numeric" function.
```{r}
is.numeric(x)
```
This function should return "TRUE" as well, which demonstrates the fact that a double is a subset of the numeric data type.
### Integer
Let's explore the "integer" data type by assigning a whole number followed by the capital letter "L" to a variable and then check its type by using the "typeof" function. Alternatively, we can use the "is.integer" function to check whether or not the variable is an integer.
```{r}
x <- 6L
# By using the "typeof" function, we can check the data type of x
typeof(x)
is.integer(x)
```
Next, let's check whether or not the variable is numeric by using the "is.numeric" function.
```{r}
is.numeric(x)
```
This function should return "TRUE" as well, demonstrating that an integer is also a subset of the numeric data type.
## Complex
Complex data types make use of the mathematical concept of an imaginary number through the use of the lowercase letter "i". The following example sets "x" equal to six times i and then displays the type of x.
```{r}
x <- 6i
typeof(x)
```
## Character
Character data types store text data. When creating characters, make sure you wrap your text in quotation marks.
```{r}
x <- "Hello!"
typeof(x)
```
## Logical
Logical data types store either "TRUE" or "FALSE". Unlike characters, these data should not be wrapped in quotation marks.
```{r}
x <- TRUE
typeof(x)
```
## Raw
Used less often, the raw data type will store data as raw bytes. You can convert character data types to raw data types by using the "charToRaw" function. Similarly, you can convert integer data types to raw data types through the use of the "intToBits" function.
```{r}
x <- charToRaw("Hello!")
print(x)
typeof(x)
x <- intToBits(6L)
print(x)
typeof(x)
```
## Resources
- W3 Schools: <https://www.w3schools.com/r/r_data_types.asp>
- "Advanced R" by Hadley Wickham: <https://adv-r.hadley.nz/vectors-chap.html#atomic-vectors>
- "Bits and Bytes" from Stanford CS 101: <https://web.stanford.edu/class/cs101/bits-bytes.html>