R for Data Scienceの例題を解く- Chapter 10 tibble

神Hadley R for Data Science の例題たちとその解答を書き残します。
今回はChapter 10 Tibblesです。




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).



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[, "xyz"]
df[, c("abc", "xyz")]
  1. 変数はxyzしかないのに、"$"が部分一致に対応しているため$xが作用してしまう。
  2. 同一のデータフレーム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?


4. Practice referring to non-syntactic names in the following data frmae by:

  1. Extracting the varible called 1.
  2. Plotting a scatterplot of 1 vs 2.
  3. Creating a new column called 3 which is 2 divided by 1.
  4. Renaming the columns to one, two, and three.
annoying <- tibble(
	`1` = 1:10,
	`2` = `1` * 2 + rnorm(length(`1`))
select(annoying, `1`) #as tibble
annoying$`1` #as vector

plot(annoying$`1`, annoying$`2`)
ggplot(annoying) + geom_point(aes(`1`, `2`))

mutate(annoying, `3` = `2`/`1`)

rename(annoying, one = `1`, two = `2`)

5. What does tibble::enframe() do? When might you use it?

変数にname, valueの二つがあり、それぞれ変換後のtibbleの列名を指定できる。

6. What option controls how many additional column names are printed at the footer of a tibble?



Chapter 10 Tibbles

R for Data Scienceの例題を解く- Chapter 10 tibble” への1件のコメント

  1. Thank you a lot for giving everyone remarkably splendid possiblity to read critical reviews from this web site. It is often so pleasant and as well , packed with a good time for me personally and my office mates to search your blog at the least thrice weekly to learn the newest secrets you have got. And of course, I’m just actually motivated considering the attractive advice served by you. Some 1 points on this page are unquestionably the most beneficial we’ve had.