1. 学ぶ
  2. /
  3. コース
  4. /
  5. 中級 R

Connected

演習

lapply に追加の引数を渡す

動画では、triple() 関数をより汎用的な multiply() 関数に書き換えました。lapply() は、multiply() 関数のように複数の引数を必要とする関数にも対応できます。

multiply <- function(x, factor) {
  x * factor
}
lapply(list(1,2,3), multiply, factor = 3)

右側のコードには、これまで作成した選択関数を汎用化した select_el() が用意されています。第1引数にベクトル、第2引数にインデックスを取り、指定したインデックスの要素を返します。

指示

100 XP

Use lapply() twice to call select_el() over all elements in split_low: once with the index equal to 1 and a second time with the index equal to 2. Assign the result to names and years, respectively.