Vectors
testing
pitches_by_innings <- c(12,15,10,20,10)
pitches_by_innings
[1] 12 15 10 20 10
strikes_by_inning <- c(9, 12, 6, 14, 9)
strikes_by_inning
[1] 9 12 6 14 9
Question_4: Define two vectors,runs_per_innings and
hits_per_innings, each with five elements.
runs_per_innings <- rep(2,5)
runs_per_innings
[1] 2 2 2 2 2
hits_per_innings <- 1:5
hits_per_innings
[1] 1 2 3 4 5
Practice sequence from 2 to 13 with intervals of 3
seq (2,13, by=3)
[1] 2 5 8 11
Adding vectors
pitches_by_innings + strikes_by_inning
Comparig vectors
pitches_by_innings == strikes_by_inning
[1] FALSE FALSE FALSE FALSE FALSE
Find the length of a vector
length(pitches_by_innings)
[1] 5
Find the minimum value of a vector
min(pitches_by_innings)
[1] 10
Find the average value in a vector
mean(pitches_by_innings)
[1] 13.4
Question_5: Get the first element of hits_per_9innings.
hits_per_innings[1]
[1] 1
Question_6: Get the last element of hits_per_9innings.
hits_per_innings[length(hits_per_innings)]
[1] 5
Vectors can have strings or logical values
player_positions <- c("catcher", "pitcher", "infielders", "outfielders")
Data Frames
data.frame ( bonus=c(2,3,1), active_roaster=c("yes","No","Yes"),
salary=c(1.5,2.5,1))
Using tables
x <- c("Yes","No", "No", "Yes", "Yes")
table(x)
x
No Yes
2 3
Numerical measures of center and spread
’’’ Suppose, MLB Teams’ CEOs yearly compensations are sampled and the
following are found (in millions)
12 .4 5 2 50 8 3 1 4 0.25 ’’’
ceo_salaries <- c(12, 0.4, 2, 15, 8, 3, 1, 4, 0.25)
Mean
mean (ceo_salaries)
[1] 5.072222
Variance
var(ceo_salaries)
[1] 28.95944
Standard Deviation
sd (ceo_salaries)
[1] 5.381398
Tukey’s Five Number Summary
fivenum(ceo_salaries)
[1] 0.25 1.00 3.00 8.00 15.00
Mode Function
getMode <- function (x) {
ux <- unique (x)
ux[which.max((tabulate(match(x, ux))))]
}
Most frequent value in pitches_by_innings
pitches_by_innings
[1] 12 15 10 20 10
getMode(pitches_by_innings)
[1] 10
strikes_by_inning
[1] 9 12 6 14 9
getMode(strikes_by_inning)
[1] 9
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.
game_day <- c("Saturday", "Saturday", "Sunday", "Monday", "Saturday","Tuesday", "Sunday", "Friday", "Friday", "Monday")
table(game_day)
game_day
Friday Monday Saturday Sunday Tuesday
2 2 3 2 1
Question_9: What is the most frequent answer recorded in the survey?
Use the getMode function to compute results.
getMode(game_day)
[1] "Saturday"
LS0tCnRpdGxlOiAiRmlyc3QgU3RlcHMgd2l0aCBSIEFjdGl2aXR5IHwgQm9zY28gTW9yYWxlcyAiCm91dHB1dDogaHRtbF9ub3RlYm9vawotLS0KCgoKCiMjIFZlY3RvcnMKCnRlc3RpbmcgCgpgYGB7cn0KcGl0Y2hlc19ieV9pbm5pbmdzIDwtIGMoMTIsMTUsMTAsMjAsMTApCnBpdGNoZXNfYnlfaW5uaW5ncwpgYGAKCmBgYHtyfQpzdHJpa2VzX2J5X2lubmluZyA8LSBjKDksIDEyLCA2LCAxNCwgOSkKc3RyaWtlc19ieV9pbm5pbmcKYGBgCiMgUXVlc3Rpb25fNDogRGVmaW5lIHR3byB2ZWN0b3JzLHJ1bnNfcGVyX2lubmluZ3MgYW5kIGhpdHNfcGVyX2lubmluZ3MsIGVhY2ggd2l0aCBmaXZlIGVsZW1lbnRzLiAKCmBgYHtyfQpydW5zX3Blcl9pbm5pbmdzIDwtIHJlcCgyLDUpCmBgYAoKYGBge3J9CnJ1bnNfcGVyX2lubmluZ3MKYGBgCmBgYHtyfQpoaXRzX3Blcl9pbm5pbmdzIDwtIDE6NQpgYGAKCmBgYHtyfQpoaXRzX3Blcl9pbm5pbmdzCmBgYAojIyBQcmFjdGljZSBzZXF1ZW5jZSBmcm9tIDIgdG8gMTMgd2l0aCBpbnRlcnZhbHMgb2YgMwoKYGBge3J9CnNlcSAoMiwgMTMsIGJ5PTMpCmBgYAojIyBBZGRpbmcgdmVjdG9ycwoKYGBge3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