1 / 6

R:追加問題の解答

R:追加問題の解答. 新納浩幸. 問題(1)の解答. player HR RBI AVG A 18 56 242 B 17 53 306 C 14 58 314 D 8 42 244 E 6 52 257 F 1 8 219 G 24 67 281 H 18 73 298 I 2 3 208 J 1 8 233 K 5 37 310 L 1 6 228 M 1 7 277 N 4 11 220 O 0 3 400 P 0 2 118 Q 0 0 250 R 0 2 261 S 0 1 188 T 50 107 334 U 12 33 318

Download Presentation

R:追加問題の解答

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. R:追加問題の解答 新納浩幸

  2. 問題(1)の解答 player HR RBI AVG A 18 56 242 B 17 53 306 C 14 58 314 D 8 42 244 E 6 52 257 F 1 8 219 G 24 67 281 H 18 73 298 I 2 3 208 J 1 8 233 K 5 37 310 L 1 6 228 M 1 7 277 N 4 11 220 O 0 3 400 P 0 2 118 Q 0 0 250 R 0 2 261 S 0 1 188 T 50 107 334 U 12 33 318 (問題1) 上記データをファイルに保存して、そのファイルから上記データを data.frame として読み込め。 df ← read.table(”datafile”,header=T)

  3. 問題(2)の解答 (問題2) itm は "HR" か "RBI" か "AVG" の値をとるとする。 問題1で作った data.frame と itm を入力し、その 平均 itm 数、最高 itm 数、最低 itm 数、最高 itm 数をもつ選手、 の4組を取り出す関数 bunseki を作成せよ。 ただし itm の入力がない場合は itm="AVG" として処理するようにせよ。 例えば、 itm="HR" なら、 8.666667, 50, 0, T の4組を返す。この4組のデータ構造は何でも良い。 bunseki <- function(df,itm="AVG") { ids <- which(df[itm] == max(df[itm])) lt <- list(mean=mean(df[itm]),max=max(df[itm]), min=min(df[itm]),maxplayer=df[ids[1],]["player"]) lt }

  4. 問題(2)の別解答 多くの人は attach を使っていました。 attach を使うと itm を文字列にしなくてすみます。 bunseki3 <- function(df,itm=AVG) { attach(df); ids <- which(itm == max(itm)) lt <- list(mean=mean(itm),max=max(itm), min=min(itm),maxplayer=player[ids[1]]) lt }

  5. which の利用例 > x ← 10:20 > x [1] 10 11 12 13 14 15 16 17 18 19 20 > y ← which(x %% 2 == 0) > y [1] 1 3 5 7 9 11 > x[y] [1] 10 12 14 16 18 20 >

  6. その他 関数はファイル( bunseki.r )に書いて、それを > source(”bunseki.r”) > と読み込んで使うのが普通です。

More Related