You can use R for basic computations you would perform in a calculator.
# Addition (Try changing the numbers below to perform other additions/subtractions)
63-23
## [1] 40
# Division (Try changing these numbers to divide different values)
5/8
## [1] 0.625
# Exponentiation (e.g. 2 raised to the power of 3)
42^-3
## [1] 1.349746e-05
# Square root
sqrt(4)
## [1] 2
# Logarithms (natural log by default)
log(32)
## [1] 3.465736
#Question_1: Compute the log base 5 of 10 and the log of 10.
# Write your code here (uncomment the lines below to test):
log(10, base = 5)
## [1] 1.430677
log(10)
## [1] 2.302585
Computing some offensive metrics in Baseball
# Batting Average = (No. of Hits) / (No. of At Bats)
# What is the batting average of a player that bats 29 hits in 112 at bats?
# (You can change 29 or 112 to calculate other batting averages)
BA = (30) / (107)
BA
## [1] 0.2803738
Batting_Average = round(BA, digits = 3)
Batting_Average
## [1] 0.28
#Question_2: What is the batting average of a player that bats 42 hits in 212 at bats?
# Write your code here (uncomment below to test):
BA_q2 = 42 / 212
round(BA_q2, digits = 3)
## [1] 0.198
# On Base Percentage
# OBP = (H + BB + HBP) / (At Bats + BB + HBP + SF)
# Let us compute the OBP for a player with the following general stats
# AB = 515, H = 172, BB = 84, HBP = 5, SF = 6
# (Feel free to edit these stats to calculate OBP for different players)
OBP = (172 + 84 + 5) / (515 + 84 + 5 + 6)
OBP
## [1] 0.4278689
On_Base_Percentage = round(OBP, digits = 3)
On_Base_Percentage
## [1] 0.428
#Question_3: Compute the OBP for a player with the following general stats:
AB = 565
H = 156
BB = 65
HBP = 3
SF = 7
# Write your code here:
OBP_q3 <- (156 + 65 + 3) / (565 + 65 + 3 + 7)
round(OBP_q3, digits = 3)
## [1] 0.35
Often you will want to test whether something is less than, greater than or equal to something.
3 == 8 # Does 3 equals 8?
## [1] FALSE
3 != 8 # Is 3 different from 8?
## [1] TRUE
3 <= 8 # Is 3 less than or equal to 8?
## [1] TRUE
3 > 4
## [1] FALSE
The logical operators are & for logical
AND, | for logical OR,
and ! for NOT. These are some
examples:
# Logical Disjunction (or)
FALSE | FALSE # False OR False
## [1] FALSE
# Logical Conjunction (and)
TRUE & FALSE # True AND False
## [1] FALSE
# Negation
! FALSE # Not False
## [1] TRUE
# Combination of statements
2 < 3 | 1 == 5 # 2<3 is True, 1==5 is False, True OR False is True
## [1] TRUE
In R, you create a variable and assign it a value using
<- as follows.
Total_Bases <- 6 + 5
Total_Bases * 3
## [1] 33
To see the variables that are currently defined, use ls
(as in “list”).
ls()
## [1] "AB" "BA" "BA_q2"
## [4] "Batting_Average" "BB" "H"
## [7] "HBP" "OBP" "OBP_q3"
## [10] "On_Base_Percentage" "SF" "Total_Bases"
To delete a variable, use rm (as in “remove”).
rm(Total_Bases)
Either <- or = can be used to assign a
value to a variable, but <- is preferred because it is
less likely to be confused with the logical comparison operator
==.
The basic type of object in R is a vector, which is an
ordered list of values of the same type. You can create a vector using
the c() function (as in “concatenate”).
# (Change the numbers in c() to define different pitches per inning)
pitches_by_innings <- c(15, 11, 11, 19, 10)
pitches_by_innings
## [1] 15 11 11 19 10
# (Change the numbers in c() to define different strikes per inning)
strikes_by_innings <- c(10, 15, 7, 12, 8)
strikes_by_innings
## [1] 10 15 7 12 8
#Question_4: Define two vectors, runs_per_9innings and hits_per_9innings, each with five elements.
# Write your code here (e.g. uncomment and edit):
runs_per_9innings <- c(3, 5, 2, 4, 1)
hits_per_9innings <- c(8, 10, 6, 9, 5)
There are also some functions that will create vectors with regular patterns, like repeated elements.
# replicate function
rep(2, 5)
## [1] 2 2 2 2 2
rep(1, 4)
## [1] 1 1 1 1
# consecutive numbers
1:5
## [1] 1 2 3 4 5
2:10
## [1] 2 3 4 5 6 7 8 9 10
# sequence from 1 to 10 with a step of 2
seq(1, 10, by = 2)
## [1] 1 3 5 7 9
seq(2, 13, by = 3)
## [1] 2 5 8 11
Many functions and operators like + or -
will work on all elements of the vector.
# add vectors element-wise
pitches_by_innings + strikes_by_innings
## [1] 25 26 18 31 18
# compare vectors element-wise
pitches_by_innings == strikes_by_innings
## [1] FALSE FALSE FALSE FALSE FALSE
# find length of vector
length(pitches_by_innings)
## [1] 5
# find minimum value in vector
min(pitches_by_innings)
## [1] 10
# find average value in vector
mean(pitches_by_innings)
## [1] 13.2
You can access parts of a vector by using [. Recall the
value of the vector pitches_by_innings:
pitches_by_innings
## [1] 15 11 11 19 10
# If you want to get the first element:
pitches_by_innings[1]
## [1] 15
#Question_5: Get the first element of hits_per_9innings.
# Write your code here:
hits_per_9innings[1]
## [1] 8
If you want to get the last element of
pitches_by_innings without explicitly typing the number of
elements of pitches_by_innings, make use of the
length function:
pitches_by_innings[length(pitches_by_innings)]
## [1] 10
#Question_6: Get the last element of hits_per_9innings.
# Write your code here:
You can also extract multiple values from a vector. For instance, to get the 2nd through 4th values:
pitches_by_innings[c(2, 3, 4)]
## [1] 11 11 19
Vectors can also contain strings or logical values.
player_positions <- c("catcher", "pitcher", "infielders", "outfielders")
player_positions
## [1] "catcher" "pitcher" "infielders" "outfielders"
In statistical applications, data is often stored as a data frame, which is like a spreadsheet, with rows as observations and columns as variables.
To manually create a data frame, use the data.frame()
function.
# (You can change values or add more columns here)
data.frame(bonus = c(2, 3, 1), # in millions
active_roster = c("yes", "no", "yes"),
salary = c(1.5, 2.5, 1)) # in millions
Most often you will be using data frames loaded from a file. For
example, load the results of a fan’s survey. The function
load or read.table can be used for this.
To randomly select a sample, use the function sample().
The following code selects 5 numbers between 1 and 10 at random (without
duplication).
# (Change the size or range to sample differently)
sample(1:10, size = 5)
## [1] 7 2 6 10 1
size =) gives the size of the
sample to select.Taking a simple random sample from a data frame is only slightly more complicated, having two steps:
sample() to select a sample of size n
from a vector of the row numbers of the data frame.[ to select those rows from the
data frame.Consider the following example with fake data. First, make
up a data frame with two columns. (LETTERS is a character
vector of length 26 with capital letters “A” to “Z”; it is automatically
pre-defined in R).
bar <- data.frame(var1 = LETTERS[1:10], var2 = 1:10)
# Check data frame
bar
Suppose you want to select a random sample of size 5. First, define a
variable n with the size of the sample, i.e., 5.
n <- 5
Now, select a sample of size 5 from the vector with 1 to 10 (the
number of rows in bar). Use the function
nrow() to find the number of rows in bar
instead of manually entering that number.
Use : to create a vector with all the integers between 1
and the number of rows in bar.
samplerows <- sample(1:nrow(bar), size = n)
# print sample rows
samplerows
## [1] 4 1 8 3 7
The variable samplerows contains the row indexes of
bar which make a random sample. Extract those rows from
bar with:
# extract rows
barsample <- bar[samplerows, ]
# print sample
print(barsample)
## var1 var2
## 4 D 4
## 1 A 1
## 8 H 8
## 3 C 3
## 7 G 7
The code above creates a new data frame called
barsample with a random sample of rows from
bar.
In a single line of code:
bar[sample(1:nrow(bar), n), ]
The table() command allows us to look at tables. Its
simplest usage looks like table(x) where x is
a categorical variable.
For example, a survey asks people if they support the home team or not. The data is:
Yes, No, No, Yes, Yes
We can enter this into R with the c() command, and
summarize with the table() command as follows:
x <- c("Yes", "No", "No", "Yes", "Yes")
table(x)
## x
## No Yes
## 2 3
Suppose, MLB Teams’ CEOs yearly compensations are sampled and the following are found (in millions):
12, 0.4, 5, 2, 50, 8, 3, 1, 4, 0.25
# (You can change these compensation numbers to calculate metrics for a different sample)
sals <- c(11, .6, 5, 16, 2, 8, 3, 10, 5, 0.25)
# the average
mean(sals)
## [1] 6.085
# the variance
var(sals)
## [1] 26.01669
# the standard deviation
sd(sals)
## [1] 5.100656
# the median
median(sals)
## [1] 5
# Tukey's five number summary, useful for boxplots
# five numbers: min, lower hinge, median, upper hinge, max
fivenum(sals)
## [1] 0.25 2.00 5.00 10.00 16.00
# summary statistics
summary(sals)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.250 2.250 5.000 6.085 9.500 16.000
In R we can write our own functions, and a first example of
a function is shown below in order to compute the mode of a
vector of observations x.
# Function to find the mode, i.e. most frequent value
getMode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
As an example, we can use the function defined above to find the most
frequent value of the number of pitches_by_innings.
# Most frequent value in pitches_by_innings
getMode(pitches_by_innings)
## [1] 11
#Question_7: Find the most frequent value of hits_per_9innings.
# Write your code here:
getMode(hits_per_9innings)
## [1] 8
#Question_8: Summarize the following survey with the `table()` command:
# What is your favorite day of the week to watch baseball? A total of 10 fans submitted this survey.
# Saturday, Saturday, Sunday, Monday, Saturday, Tuesday, Sunday, Friday, Friday, Monday
game_day <- c("Saturday", "Saturday", "Sunday", "Monday", "Saturday", "Tuesday", "Sunday", "Friday", "Friday", "Monday")
# Write your table code here:
table(game_day)
#Question_9: What is the most frequent answer recorded in the survey? Use the getMode function to compute results.
# Write your code here (uncomment below to test):
getMode(game_day)
## [1] "Saturday"