-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson2-variable-types.Rmd
184 lines (119 loc) · 4.69 KB
/
lesson2-variable-types.Rmd
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
---
title: "2. Variable Types"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)
```
In this lesson we will introduce different types of variables, including character, numeric, integer, logical.
## Types of Variables
### Character
- A character is a type of *class* that contains a string of characters. For example, a name is a string of characters (or letters).
- In the example below, I've saved my name "Jenny" to a variable I've called "my_char".
```{r}
my_char <- "Jenny"
```
- To check what type of variable you have, you can use the `class()` function
- All we have to do is insert our variable (my_char) within the parentheses of our function
*Note, we will review functions in a following lesson, but for now just run the below line of code and see what gets returned*
```{r}
class(my_char)
```
- As you can see, R returned the word "character" as we may have expected
- We can also use the `is.character()` function to check if our variable is a character. This function will return either TRUE or FALSE
```{r}
is.character(my_char)
```
### Numeric
- Numeric is a type of *class* that contains whole numbers *or* numbers with decimals
- Let's test out a couple of examples
- We will first assign a number to a new variable (my_num1 or my_num2)
- And then we'll ask R to print (or return) the value for us
```{r}
my_num1 <- 22 # my favorite number!
my_num1
my_num2 <- 22.22
my_num2
```
- Let's check the classes of the variables we've just created
```{r}
class(my_num1)
class(my_num2)
```
- We can see that both of our variables are numeric
### Integer
- An integer is a type of *class* that contains *only* whole numbers
- Of course, this is similar to the numeric class, but without decimals. So how does R know if a whole number is numeric or an integer?
- By default, R will assign any whole number to the numeric class. We observed this above when we saw the class of the number 22 was numeric
- However, as we'll see below, you can convert a numeric class to an integer using the `as.integer()` function
```{r}
my_int1 <- as.integer(22)
class(my_int1)
```
- But, what happens if we try to convert a number with a decimal to an integer?
```{r}
my_int2 <- as.integer(22.22)
class(my_int2)
```
- This works! It has successfully converted 22.22 to an integer
- However, it is important to note that 22.22 was forced to become a whole number
- So, if we look at my_int2, it is now the whole number 22 rather than the decimal 22.22
```{r}
my_int2
```
### Logical
- Logical is a special type of *class* that contains only two possible values: TRUE or FALSE
- Please note that the logical values must be written in all caps with no quotes
```{r}
log1 <- TRUE
log2 <- FALSE
```
- Once again, we'll check the classes of the variables we've just created
```{r}
class(log1)
class(log2)
```
- What happens if we try the following?
```{r}
log3 <- "TRUE"
class(log3)
```
- Because TRUE is in "" here, by default R assigns this to the character class
```{r eval=FALSE}
log4 <- true
```
- This example doesn't even run! R gives us an error here saying "object 'true' not found"
## Summary
In this lesson, we learned about different types of variables. We specifically focused on 4 of the most common variables:
- Character: list of character strings
- Numeric: whole numbers OR numbers with decimals
- Integer: Only whole numbers
- Logical: TRUE or FALSE
## Exercises
In this exercise, we'll return to our three fabulous female artists: Jennifer Lopez, Taylor Swift, and Miley Cyrus. In the exercises in lesson we, we compared their heights, which we can see below.
*Remember to run the code before moving on.*
```{r}
jennifer <- 64.57
taylor <- 70.87
miley <- 64.96
```
1. What kind of variables are we dealing with here? Are they characters, numeric, integers, or logical? Once you think you know the answer, in the R Chunk below, use the `class()` function to see if you're right. Test it out with any or all of the artists.
```{r}
```
2. Now, convert Jennifer's height to an integer. What number number is returned?
```{r}
```
3. Save your results to a new variable `jennifer_int`. Do the same steps for Miley and save the result to `miley_int`.
```{r}
```
4. When we're dealing with integers, are Miley and Jennifer the same height?
```{r}
```
5. Create three new variables with each woman's respective full names (first and last). Save your three variables as`jennifer_name`, `taylor_name`, and `miley_name`.
*Hint: ff you get an error that says "object not found", return to the very first R Chunk in this lesson and see how I saved my name.*
```{r}
```
What type of variables are these? Test it out with any or all of the artists.
```{r}
```
## THE END