Loading

블로그채널

Search !

[R] Chap2 Ex4 subset function strikeout-walk ratios


Chapter 2. Exercises 4. 
subset function strikeout-walk ratios

Analyzing Baseball Data with R, Introduction to R, page 58


 

(a) In R, place the strikeout and walk totals from the 350 win pitchers in the vectors SO and BB, respectively. Also, create a character vector Name containing the last names of these pitchers.

SO <- c(2198, 4672, 1806, 3509, 3371, 2502, 1868, 2583, 2803)
BB <- c(951, 1580, 745, 1363, 999, 844, 1268, 1434, 1217)
Name <- c("Alexander", "Clemens", "Galvin", "Johnson", "Maddux", "Mathewson", "Nichols", "Spahn", "Young")

d2a34c366639589107a79f26f3ec2ba5

(b) Compute the strikeout-walk ratio by SO / BB and put these ratios in the vector SO.BB.Ratio.

SO.BB.Ratio <- SO / BB
> SO.BB.Ratio
[1] 2.311251 2.956962 2.424161 2.574468 3.374374 2.964455 1.473186 1.801255 2.303205 

 

(c) By use of the command

SO.BB <- data.frame(Name, SO, BB, SO.BB.Ratio)

create a data frame SO.BB containing the names, strikeouts, walks, and strikeout-walk ratios.

 

[R] Chap2 Ex4 C subset function strikeout-walk ratios

 

 

(d) By use of the subset function, find the pitchers who had a strikeout-walk ratio exceeding 2.8.

subset(SO.BB, SO.BB.Ratio > 2.8)

Clemens, Maddux and Mathewson had a strikeout-walk ratio exceeding 2.8.

[R] Chap2 Ex4 e subset function strikeout-walk ratios

 

(e) By use of the order function, sort the data frame by the number of walks. Did the pitcher with the largest number of walks have a high or low strikeout-walk ratio?

No, the pitcher with the largest number of walks didn't have a high or low strikeout-walk ratio.

SO.BB <- SO.BB [order(SO.BB$BB, decreasing=TRUE), ]

 

Chap2 Ex4 subset function strikeout-walk ratios