2.4 Vectors

A sequence of values of the same type (e.g. numeric or character).

If you include multiple types, R will automatically force same type.

# Spahn's wins and losses after the war (this is a code comment)

W <- c(8, 21, 15, 21, 21, 22, 14)
L <- c(5, 10, 12, 14, 17, 14, 19)

win_pct <- 100 * W / (W + L)
Year <- seq(from = 1946, to = 1952) # Same: Year <- 1946:1952

R has a lot of built-in functions for vectors

# total wins over post-war span
sum(W)
## [1] 122
# number of seasons post-war
length(W)
## [1] 7
# avg. winning pct.
mean(win_pct)
## [1] 57.66207

Ways to select data with vector index and logicals.

W[c(1, 2, 5)]
## [1]  8 21 21
W[1 : 4]
## [1]  8 21 15 21
W[-c(1, 6)]
## [1] 21 15 21 21 14

How many times did Spahn exceed 20 wins? What years?

W > 20
## [1] FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE
sum(W > 20)
## [1] 4
Year[W > 20]
## [1] 1947 1949 1950 1951