Chapter 2. Exercises 2.
Character, Factor, and Logical Variables in R
Analyzing Baseball Data with R, Introduction to R, page 56
Suppose one records the outcomes of a batter in ten plate appearances: Single, Out, Out, Single, Double, Out, Walk, Out, Single
(a) Use the c function to collect these outcomes in a character vector outcomes.
> outcomes = c("Single", "Out", "Out", "Single", "Double", "Out", "Walk", "Out", "Single")
(b) Use the table function to construct a frequency table of outcomes.
> table(outcomes)
outcomes
Double Out Single Walk
1 4 3 1
(c) In tabulating these results, suppose one prefers the results to be ordered from least-successful to most-successful. Use the following code to convert the character vector outcomes to a factor variable f.outcomes.
f.outcomes <- factor(outcomes, levels = c("Out", "Walk", "Single", "Double"))
Use the table function to tabulate the value in f.outcomes. How does the output differ from what you saw in part (b)?
> table (f.outcomes)
f.outcomes
Out Walk Single Double
4 1 3 1
(d) Suppose you want to focus only on the walks in the plate appearances. Describe what is done in each of the following statements.
outcomes == "Walk"
[1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
sum(outcomes == "Walk")
[1] 1
'컴퓨터 언어 > R' 카테고리의 다른 글
[R] Chap2 Ex5 Pitcher Strikeout / Walk Ratios (0) | 2015.02.04 |
---|---|
[R] Chap2 Ex4 subset function strikeout-walk ratios (0) | 2015.02.01 |
[R] Chap2 Ex3 Pitchers in the 350 Wins Club (0) | 2015.01.31 |
[R] Chap2 Ex1 Top Base Stealers in the Hall of Frame (0) | 2015.01.29 |
R 통계 프로그램 사용법 및 설치 매뉴얼 다운로드 소개 (11) | 2015.01.27 |