Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 243
7.3. Working with the Tidyverse 7.3.1 Tibbles
ОглавлениеTibbles are in many aspects a special type of data frames. The do the same as data frames (i.e. store rectangular data), but they have some advantages.
Let us dive in and create a tibble. Imagine for example that we want to show the sum of the sine and cosine functions. The output of the code below is in Figure 7.1 on this page.
x <- seq(from = 0, to = 2 * pi, length.out = 100) s <- sin(x) c <- cos(x) z <- s + c plot(x, z, type = “l”,col=“red”, lwd=7) lines(x, c, col = “blue”, lwd = 1.5) lines(x, s, col = “darkolivegreen”, lwd = 1.5)
Figure 7.1: The sum of sine and cosine illustrated.
Imagine further that our purpose is not only to plot these functions, but to use them in other applications. Then it would make sense to put them in a data, frame. The following code does exactly the same using a data frame.
x <- seq(from = 0, to = 2 * pi, length.out = 100) #df <- as.data.frame((x)) df <- rbind(as.data.frame((x)),cos(x),sin(x), cos(x) + sin(x)) # plot etc.
This is already more concise. With the tidyverse, it would look as follows (still without using the piping):
library(tidyverse) x <- seq(from = 0, to = 2 * pi, length.out = 100) tb <- tibble(x, sin(x), cos(x), cos(x) + sin(x))
The code below first prints the tibble in the console and then plots the results in Figure 7.2 on this page.
Figure 7.2: A tibble plots itself like a data-frame.
The code with a tibble is just a notch shorter, but that is not the point here. Themain advantage in using a tibble is that it will usually do things that make more sense for the modern R-user. For example, consider how a tibble prints itself (compared to what a data frame does).
# Note how concise and relevant the output is: print(tb) ## # A tibble: 100 x 4 ## x `sin(x)` `cos(x)` `cos(x) + sin(x)` ## <dbl> <dbl> <dbl> <dbl> ## 1 0 0 1 1 ## 2 0.0635 0.0634 0.998 1.06 ## 3 0.127 0.127 0.992 1.12 ## 4 0.190 0.189 0.982 1.17 ## 5 0.254 0.251 0.968 1.22 ## 6 0.317 0.312 0.950 1.26 ## 7 0.381 0.372 0.928 1.30 ## 8 0.444 0.430 0.903 1.33 ## 9 0.508 0.486 0.874 1.36 ## 10 0.571 0.541 0.841 1.38 ## # … with 90 more rows # This does the same as for a data-frame: plot(tb) # Actually a tibble will still behave as a data frame: is.data.frame(tb) ## [1] TRUE