神Hadley R for Data Science の例題たちとその解答を書き残します。
今回はChapter 10 Tibblesです。
過去の記事
この章では下記のライブラリを使う。
library(tidyverse)
Chapter 10 Tibbles
10.5 Exercises
1. How can you tell if an object is a tibble? (Hint: try printing
macars
, which is a rgular data frame).
is_tibble(mtcars) # FALSE is_tibble(diamonds) # TRUE
2. Compare and contrast the following operations on a
data.frame
and equivalent tibble. What is different? Why might the default data frame behaviors cause you frustration?df <- data.frame(abc = 1, xyz = "a") df$x df[, "xyz"] df[, c("abc", "xyz")]
- 変数は
xyz
しかないのに、"$"
が部分一致に対応しているため$x
が作用してしまう。 - 同一のデータフレーム
df
に同一の関数"["
を作用させているのに、引数の数の違い(ここでは"xyz"
とc("abc", "xyz")
の違い)によって関数が返す値がベクトルとデータフレームと変化する。
3. If you have the name of a variable stored in an object, e.g.
var <- "mpg"
, how can you extract the reference varible from a tibble?
as_tibble(mtcars)[var]
4. Practice referring to non-syntactic names in the following data frmae by:
- Extracting the varible called
1
.- Plotting a scatterplot of
1
vs2
.- Creating a new column called
3
which is2
divided by1
.- Renaming the columns to
one, two
, andthree
.annoying <- tibble( `1` = 1:10, `2` = `1` * 2 + rnorm(length(`1`)) )
#1. select(annoying, `1`) #as tibble annoying$`1` #as vector #2. plot(annoying$`1`, annoying$`2`) ggplot(annoying) + geom_point(aes(`1`, `2`)) #3. mutate(annoying, `3` = `2`/`1`) #4. rename(annoying, one = `1`, two = `2`)
5. What does
tibble::enframe()
do? When might you use it?
名前付きベクトルを名前と値の二つの列を持つtibbleに変換する関数。
変数にname, value
の二つがあり、それぞれ変換後のtibbleの列名を指定できる。
6. What option controls how many additional column names are printed at the footer of a tibble?
print
関数のn_extra
オプションでコントロールする。