-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path02_getting_data_04_dataframes.Rmd
More file actions
179 lines (117 loc) · 6.9 KB
/
Copy path02_getting_data_04_dataframes.Rmd
File metadata and controls
179 lines (117 loc) · 6.9 KB
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
```{r, include = FALSE}
ottrpal::set_knitr_image_path()
```
# Data Frames
A majority of the time that you are working with data in R you will have it in the form of a data frame.
Data frames can contain different types of variables in a rectangular format, much like how spreadsheets are.
A data frames' size can be described in terms of rows (across) and columns (up/down). Rows are often called *observations* and columns are called *variables*.
```{r, out.width = "100%"}
ottrpal::include_slide("https://docs.google.com/presentation/d/1Q47qnIkVzE-JzCEE5Lm54P6yqReg09QJdr7kiFyCbGc/edit#slide=id.g313d649efe_0_38")
```
\*Much of this chapter is paraphrased or inspired by [content from the Childhood Cancer Data Lab](https://github.com/AlexsLemonade/training-modules/blob/master/intro-to-R-tidyverse/01-intro_to_base_R.Rmd).
## Exploring data frames
In R there are built in data frames that you can use to play with. In this lesson, we will use some of these to get comfortable with data frames.
As a data scientist, one of your main skills will be constantly looking at and evaluating your data! In RStudio, you can see your data frame by clicking on the name of your data frame in the *Environment* pane.
Some useful functions for exploring our data frame include:
- `head()` to see the first 6 rows of a data frame. Additional arguments supplied can change the number of rows.
- `tail()` to see the last 6 rows of a data frame. Additional arguments supplied can change the number of rows.
- `names()` to see the column names of the data frame.
- `nrow()` to see how many rows are in the data frame
- `ncol()` to see how many columns are in the data frame.
Try out these functions to explore your data frame by putting the iris data frame in them.
```
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
```
We can additionally explore overall properties of the data frame with two different functions: `summary()` and `str()`.
The `summary()` function gives us a summary of each variable in the data frame.
```{r}
summary(iris)
```
`str()` function gives us information about the structure and variables included in the `iris` data frame.
```{r}
str(iris)
```
### Subsetting data frames
There are multiple ways you can subset your data frame if you want to only look at certain parts of it.
#### $ for extracting columns
Each column in this data frame can behave as a vector and we can easily pull it out from the data frame by using a `$`.
```{r}
iris$Species
```
Note that `$` work for any R objects that have column names. But don't work if there aren't column names. When you look at a data frame you will see that there are names to the columns. Try using the `colnames()` function to explore the column names of the `iris` data frame.
```{r}
colnames(iris)
```
#### Brackets for indexing
Brackets can be used to index data frames or other objects. In the case of data frames, you can subset data frames like below:
In this example, this will extract the second row's data and the 1st column.
```{r}
iris[2, 1]
```
Subsetting always goes `[row, column]`.
If we want the whole row or whole column to be extract we can leave the other spot blank like this:
In this example, the first row and all the columns of that row will be extracted.
```{r}
iris[1, ]
```
In this example, all rows of the 3rd column will be extracted.
```{r}
iris[, 3]
```
### First plot!
Now that we have some basics down, let's preview how we can easily make visuals with R. "A picture is worth a thousand words" is also true when it comes to data!
In this quick plot example, we can show how `Sepal Length` is related to `Sepal Width` by extracting the data using `$`. Additionally we can color code the data points by `Species` by using the `col` argument.
```{r}
plot(iris$Sepal.Length, iris$Sepal.Width, col = iris$Species)
```
Don't worry too much about the specifics of plotting. We will go into much more detail about how to make visuals of your data later. This is just to illustrate how R makes data visuals easy to make!
### What about matrices and tibbles?
At this point, we should mention some cousins of the data frame: matrices and tibbles.
All three of these types of objects: data frames, matrices, and tibbles contain two dimensions: rows and columns. And often you may be able to handle them similarly. But, they do have some differences that are worth knowing about!
#### What's a matrix?
A matrix is like a data frame but it needs to contain all the same data type. So it can be all numeric, all character or etc.
A data frame by contrast can have one column that is string, another that is factor, and so on.
Let's experiment with what this means in practical terms and start out by making a very small data frame.
```{r}
df <- data.frame(num = 1:4, y = c("red", "blue", "green", "purple"))
df
```
```{r}
str(df)
```
We've made our data frame with two types of data: a numeric (integer) column and a character column.
Let's see what happens if we coerce this to a matrix
```{r}
a_matrix <- as.matrix(df)
a_matrix
```
```{r}
str(a_matrix)
```
Notice that this whole matrix is now a character but still does have two dimensions!
#### What's a tibble?
A tibble is super similar to a data frame. It's the tidyverse brand of data frame.
According to the people who develop the tidyverse:
> Tibbles are data.frames that are lazy and surly: they do less (i.e. they don’t change variable names or types, and don’t do partial matching) and complain more (e.g. when a variable does not exist).
They are similar enough, that for most instances, especially when you are working with tidyverse functions, you can treat them the same. However if you get a peculiar error when trying to use a tibble with a non-tidyverse function, you may need to use the `as.data.frame()` function to coerce your tibble to a data frame. But you shouldn't encounter this problem too often.
<div class = "dictionary">
- `observations`- are also called rows.
- `variables`- are also called columns.`
- `Environment- where you can see your data frame.`
- `head()`- to see the first 6 rows of a data frame.`
- `tail()`- to see the last 6 rows of a data frame.`
- `names()`- to see the column names of the data frame.`
- `nrow()`- to see how many rows are in the data frame`
- `ncol()`- to see how many columns are in the data frame.`
- `summary()`- function gives us a summary of each variable in the data frame.`
- `str()`- function gives us information about the structure and variables.`
- `matrix - is like a data frame but it needs to contain all the same data type.`
- `tibble- similar to a data frame. It is the tidyverse brand of data frame.`
</div>