#Let us complete some basic opereation Using R
1+2
[1] 3
1-4
[1] -3
2*5
[1] 10
sqrt(9)
[1] 3
log(10)
[1] 2.302585
log(2.72)#Natural Log (Ln)
[1] 1.000632
log10(5)
[1] 0.69897
log10(10)
[1] 1
#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?
BA=(29)/(112)
BA
[1] 0.2589286
Batting_Average=round(BA,digits = 3)
Batting_Average
[1] 0.259
#Question_2:What is the batting average of a player that bats 42 hits in 212 at bats?
BA=(42)/(212)
BA
[1] 0.1981132
Batting_Average=round(BA,digits = 3)
Batting_Average
[1] 0.198
OBP
#On Base Percentage
#Let us compute the OBP for a player with the following general stats
#AB=515,H=172,BB=84,HBP=5,SF=6
OBP=(172+84+5)/ (515+172+84+5+6)
OBP
[1] 0.3337596
On_Base_Percentage=round(OBP,digits = 3)
On_Base_Percentage
[1] 0.334
OBP
#Compute the OBP for a player with the following general stats:
#AB=565,H=156,BB=65,HBP=3,SF=7
OBP=(152+65+3)/(565+152+65+3+6)
OBP
[1] 0.278129
On_Base_Percentage=round(OBP,digits = 3)
On_Base_Percentage
[1] 0.278
3 == 8# Does 3 equals 8?
[1] FALSE
3 !=8# Is 3 different From 8?
[1] TRUE
3>4
[1] FALSE
# 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
Total_Bases <- 6 + 5
Total_Bases*3
[1] 33
ls()
[1] "BA" "Batting_Average"
[3] "OBP" "On_Base_Percentage"
[5] "pitches_by_innings" "strikes_by_innings"
rm("Total_Bases")
Warning in rm("Total_Bases") : object 'Total_Bases' not found
pitches_by_innings <- c(12, 15, 10, 20, 10)
pitches_by_innings
[1] 12 15 10 20 10
strikes_by_innings <- c(9, 12, 6, 14, 9)
strikes_by_innings
[1] 9 12 6 14 9
#Question_4: Define two vectors,runs_per_9innings and hits_per_9innings, each with five elements.
runs_per_9innings <- c(11, 13, 6, 9, 11)
runs_per_9innings
[1] 11 13 6 9 11
hits_per_9innings <- c(13, 5, 7, 10, 20)
hits_per_9innings
[1] 13 5 7 10 20
# replicate function
rep(2, 5)
[1] 2 2 2 2 2
# consecutive numbers
0:5
[1] 0 1 2 3 4 5
# sequence from 1 to 20 with a step of 5
seq(1, 20, by=5)
[1] 1 6 11 16
# add vectors
pitches_by_innings+ runs_per_9innings
[1] 23 28 16 29 21
# compare vectors
hits_per_9innings == strikes_by_innings
[1] FALSE FALSE FALSE FALSE FALSE
# find length of vector
length(hits_per_9innings)
[1] 5
# find minimum value in vector
min(strikes_by_innings)
[1] 6
# find average value in vector
mean(pitches_by_innings)
[1] 13.4
pitches_by_innings
[1] 12 15 10 20 10
#Question_5: Get the first element of hits_per_9innings.
hits_per_9innings[1]
[1] 13
#Question_6: Get the last element of hits_per_9innings.
hits_per_9innings[length(hits_per_9innings)]
[1] 20
data.frame(bonus = c(2, 3, 1),#in millions
active_roster = c("yes", "no", "yes"),
salary = c(1.5, 2.5, 1))#in millions
sample(1:16, size=5)
[1] 14 5 4 7 3
bar <- data.frame(var1 = LETTERS[1:10], var2 = 1:10)
# Check data frame
bar
samplerows <- sample(1:nrow(bar), size=n)
# print sample rows
samplerows
[1] 5 9 10 1 7
# extract rows
barsample <- bar[samplerows, ]
# print sample
print(barsample)
bar[sample(1:nrow(bar), n), ]
x <- c("Yes","No","No","Yes","Yes")
table(x)
x
No Yes
2 3
sals <- c(12, .4, 5, 2, 50, 8, 3, 1, 4, 0.25)
# the average
mean(sals)
[1] 8.565
# the variance
var(sals)
[1] 225.5145
# the standard deviation
sd(sals)
[1] 15.01714
# the median
median(sals)
[1] 3.5
# Tukey's five number summary, usefull for boxplots
# five numbers: min, lower hinge, median, upper hinge, max
fivenum(sals)
[1] 0.25 1.00 3.50 8.00 50.00
# summary statistics
summary(sals)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.250 1.250 3.500 8.565 7.250 50.000
# Most frequent value inhits_per_9innings
getMode(hits_per_9innings)
[1] 13
#Question_8: Summarize the following survey with the `table()` command:
table (game_day <-c("Saturday", "Saturday", "Sunday", "Monday", "Saturday","Tuesday", "Sunday", "Friday", "Friday", "Monday"))
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"