神Hadley R for Data Science の例題たちとその解答を書き残します。
今回はChapter 20 Vectorsです。
過去の記事
- Chapter 3 データ可視化
- Chapter 5 データ変換
- Chapter 7 探索的データ分析
- Chapter 10 tibble
- Chapter 11 Data import
- Chapter 12 Tidy data
- Chapter 13 関係データ
- Chapter 14 文字列
- Chapter 15 ファクタ
- Chapter 16 日付と時刻
- Chapter 19 関数
20 Vectors
20.3 Important types of atomic vector
20.3.5 Exercises
1. Describe the difference between
is.finite(x)
and!is.infinite(x)
.
is.finite
は有限の数値のみでTRUE
を返す。
!is.infinite
は有限の数値、NA
, NaN
でTRUE
を返す。
2. Read the source code for
dplyr::near()
. How does it work?
差が.Machine$double.eps
の平方根よりも小さければTRUE
を返す。
.Machine$double.eps
はマシンが持つdoubleの最小単位で、1 + x != 1
を区別できる最小値として定義されている。
通常は2.220446e-16
3. A logical vector can take 3 possible values. How many possible values can an integer vector take? How many possible values can a double take? Use google to do some research.
helpによるとintegerは32bit。よって2^32=4294967296種類の数値を表せる。これにNA
が加わるため合計4294967297種類の値を取る。
doubleは浮動小数点でhelpによるとIEEE754に準拠している。wikipediaを見ると数値は (-1)^s \times c\times b^{q}の形で表現され、s = \pm 1, c = 0\sim 2^{53}-1, b = 2, q=-1022\sim1023だそうだ。
ということは表現できる数値の種類は2^{53} * 2045プラスのNA, NaN, Inf, -Inf
の4を加えた数字。1.841972e+19
程度
4. Brainstorm at least four functions that allow you to convert a double to an integer. How do they differ? Be precise.
round(x, digits = 0)
は一番近い値に丸める。ほぼ四捨五入と同じ。
ceiling(x)
はx
未満でない最小の整数を返す。
floor(x)
はx
以上でない最大の整数を返す。
trunc(x)
はゼロに向かって整数化する。
5. What functions from the readr package allow you to turn a string into logical, integer and double vector?
parse_***
20.4 Using atomic vectors
20.4.6 Exercises
1. What does
mean(is.na(x))
tell you about a vectorx
? What aboutsum(!is.finite(x))
?
mean(is.na(x))
はベクトルx
に占めるNA
の割合を意味する。
sum(!is.finite(x))
は、x
がcharacter以外ならば、Inf, Na, NaN
の数を返す。
もしcharacterならばベクトルの長さを返す。
2. Carefully read the documentation of
is.vector()
. What does it actually test for? Why doesis.atomic()
not agree with the definition of atomic vectors above?
helpによるとis.vector()
はname以外のattributeを持たないことのようだ。
だからたとえば日付ベクトルはアウト。
is.vector(as.Date("2019-01-01")) # [1]FALSE
is.atomic
はアトミックベクトルとNULL
でTRUE
を返す。なぜNULL
なのか・・・
一方purrr::is_atomic
はNULL
はちゃんとFALSE
3. Compare and contrast
setNames()
withpurrr::set_names()
基本機能は同じだが、デフォルトの動作が微妙に違う。
setNames
は名前を付けたいベクトルと、名前となる文字列ベクトルの必ず両方を指定する。
set_names
は名前を付けたいベクトルだけだと、要素と同じ文字列で名前が指定される。
set_names
は関数を使って名前を変換することもできる!すげえ
purrr::set_names(mtcars, toupper)
これで元々の名前を大文字に変換できる。
4. Create functions that take a vector as input and returns:
- The last value.
- The elements at even numbered positions.
- Every element except the last value.
- Only even numbers (and no missing values).
#1 x[[length(x)]] #2 x[seq(2, length(x), by = 2)] #3 x[-length(x)] #4 x[x %% 2 == 0 & !is.na(x)]
5. Why is
x[-which(x > 0)]
not the same asx[x <= 0]
>
x[-which(x > 0)]
はNaN
が残る。
x[x <= 0]
はNaN
が残らない。
6. What happens when you subset with a positive integer that's bigger than the length of the vector? What happens when you subset with a name that doesn't exist?
どちらもNA
を返す。
20.5 Recursive vectors (lists)
20.5.4 Exercises
1. Draw the following lists as nested sets:
list(a, b, list(c, d), list(e, f))
list(list(list(list(list(list(a))))))
2. What happens if you subset a tibble as if you’re subsetting a list? What are the key differences between a list and a tibble?
tibbleの[
はtibbleを返し、[[
はatomic vectorを返す。
20.7 Augmented vectors
20.7.4 Exercises
4. What does
hms::hms(3600)
return? How does it print? What primitive type is the augmented vector builton top of? What attributes does it use?
(a <- hms::hms(3600)) # 01:00:00 typeof(a) # [1] "double" attributes(a) #$units #[1] "secs" #$class #[1] "hms" "difftime"
2. Try and make a tibble that has columns with different length. What happens?
tibble(a = 1:3, b = 1:5) #Error: Column `a` must be length 1 or 5, not 3
3. Based on the definition above, is it ok to have a list as a column of a tibble?
tibbleの定義では要素は同じ長さのベクトルでなければならない。
listは(アトミックでは無くとも)ベクトルなので、listの要素数が他の変数のベクトルの長さと同じであればokである。