-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04_tibble.Rmd
167 lines (134 loc) · 3.17 KB
/
04_tibble.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
---
title: "04_tibble"
output: html_document
---
class: middle, center, inverse
layout: false
# 4.2 `tibble`:<br><br>Simple Data Frames
---
background-image: url(https://raw.githubusercontent.com/tidyverse/tibble/master/man/figures/logo.png)
background-position: 97.5% 2.5%
background-size: 7.5%
layout: true
---
## 4.2 `tibble`: Simple Data Frames
`tibble` provides an enhanced data frame object of class `tbl_df`, a so-called `tibble`. A `tibble` can be created in four different ways.
.panelset[
.panel[.panel-name[tibble()]
Create a `tibble` from column vectors with `tibble()`.
```{r}
tibble(
x = c("a", "b"),
y = c(1, 2),
z = c(T, F)
)
```
]
.panel[.panel-name[tribble()]
Create a *transposed* `tibble` row by row with `tribble()`.
```{r}
tribble(
~x, ~y, ~z,
"a", 1, T,
"b", 2, F
)
```
]
.panel[.panel-name[as_tibble()]
Create a `tibble` from an existing data frame with `as_tibble()`.
```{r}
data.frame(
x = c("a", "b"),
y = c(1, 2),
z = c(T, F)
) %>%
as_tibble
```
]
.panel[.panel-name[enframe()]
Create a `tibble` from named vectors with `enframe()`.
```{r}
c(x = "a", y = "b", z = 1) %>%
enframe(name = "x", value = "y")
```
]
]
--
There are three important differences between a `tibble` and a `data.frame` object.
???
- named vector: i have key-value pairs
---
## 4.2 `tibble`: Simple Data Frames
**Printing:** By default, `tibble()` prints only the first ten rows and all the columns that fit on the screen as well as a description of the data type. This gives you a much more concise view of your data.
```{r}
penguins
```
???
- you will never again have the problem that `R` takes minutes to print a large data frame entirely to your console (`reached 'max' / getOption("max.print")`)
---
## 4.2 `tibble`: Simple Data Frames
**Printing:** By default, `tibble()` prints only the first ten rows and all the columns that fit on the screen as well as a description of the data type.
.panelset[
.panel[.panel-name[data.frame()]
```{r, echo=F}
options(max.print=40)
```
```{r}
data.frame(penguins)
```
```{r, echo=F}
options(max.print=1000)
```
]
.panel[.panel-name[tibble() (Option 1)]
```{r}
penguins
```
]
.panel[.panel-name[tibble() (Option 2)]
```{r}
penguins %>% glimpse
```
]
]
???
- in contrast to `data.frame()` which prints an extensive number of rows (wh)
`glimpse` transposed version of `print()`
---
## 4.2 `tibble`: Simple Data Frames
**Subsetting:** Subsetting a `tibble` (`[]`) always returns another `tibble` and never a vector (in contrast to standard `data.frame` objects).
.panelset[
.panel[.panel-name[data.frame()]
```{r}
data.frame(penguins) %>% .[,"species"] %>% class
```
]
.panel[.panel-name[tibble()]
```{r}
penguins[,"species"] %>% class
```
]
]
---
## 4.2 `tibble`: Simple Data Frames
**Partial Matching:** Subsetting a `tibble` does not allow for partial matching, i.e. you must always provide the whole column name.
.panelset[
.panel[.panel-name[data.frame()]
```{r, echo=F}
options(max.print=40)
```
```{r}
data.frame(penguins)$spec
```
```{r, echo=F}
options(max.print=1000)
```
]
.panel[.panel-name[tibble()]
```{r}
penguins$spec
```
]
]
???
- also an advantage of tibbles: Giving you better warning messages to confront you with problems early on.