1. Create a vector that contains 20 numbers. (You may choose whatever numbers you like, but make sure there are some duplicates.)
# Jet win total for the past 20 years
vector_num1 = c(2,7,4,5,5,10,4,8,6,8,11,9,9,4,10,4,10,6,9,10)
length(vector_num1)           # 20
## [1] 20



  1. Use R to convert the vector from question 1 into a character vector.
vector_char1 <- as.character(vector_num1)

class(vector_char1)            # character
## [1] "character"



3. Use R to convert the vector from question 1 into a vector of factors

vector_factor1<-factor(vector_char1)
class(vector_factor1)       # factor
## [1] "factor"



4. Use R to show how many levels the vector in the previous question has.

# note: R puts 10 and 11 before 2 since we created it from characters
nlevels(vector_factor1)  # 9
## [1] 9
levels(vector_factor1)   #  "10" "11" "2"  "4"  "5"  "6"  "7"  "8"  "9"
## [1] "10" "11" "2"  "4"  "5"  "6"  "7"  "8"  "9"



5. Use R to create a vector that takes the vector from question 1 and performs on it the formula 3^2−4X+1.

(3*vector_num1^2) - (4*vector_num1)  + 1
##  [1]   5 120  33  56  56 261  33 161  85 161 320 208 208  33 261  33 261  85 208
## [20] 261



6. Create a named list.

jets_defense = list(dline=c("Williams","Fatukasi","Anderson"), linebackers =c("Hewitt","Jenkins","Langi","Williamson"), secondary= c("Poole","Austin","Maye","Desir") )

names(jets_defense)
## [1] "dline"       "linebackers" "secondary"
jets_defense["dline"]
## $dline
## [1] "Williams" "Fatukasi" "Anderson"



7. Create a data frame with four columns – one each character, factor (with three levels), numeric, and date.

player = c("Norm Van Brocklin", "Matt Schaub", "Warren Moon", "Boomer Esiason")
era = c("Fifties","Current","Nineties","Nineties")
yards = c(554,527,527,522)
date = c(as.Date("1951-09-28"),as.Date("2012-11-18"), as.Date("1990-12-16"), as.Date("1996-11-10"))

 # note: my data frames converted to character by default so here i have to explicitly call factor()
qb_best_games <- data.frame(player,era = factor(era), yards, date)



8. Illustrate how to add a row with a value for the factor column that isn’t already in the list of levels.

# this requires to replace the era column with itself, but specify the levels with the new level added
lvls <- levels(qb_best_games$era)
lvls[length(lvls) + 1] <- 'Eighties'
qb_best_games$era <- factor(qb_best_games$era, levels = lvls)

qb_best_games<-rbind(qb_best_games,data.frame(player= "Phil Sims", era= factor("Eighties"), yards = 513, date= as.Date('1085-10-13')))



9. Show the code that would read in a CSV file called temperatures.csv

qb_games <- read.csv("C:\\Users\\arono\\Documents\\R\\SportsStats\\qb_best_games.csv")

head(qb_games,10)
##    Rank             Player Yds  Tm       Date Decade
## 1     1  Norm Van Brocklin 554 RAM 1951-09-28    50s
## 2     2        Matt Schaub 527 HOU 2012-11-18    10s
## 3     3        Warren Moon 527 HOU 1990-12-16    90s
## 4     4     Boomer Esiason 522 ARI 1986-11-10    80s
## 5     5 Ben Roethlisberger 522 PIT 2014-10-26    10s
## 6     6         Dan Marino 521 MIA 1988-10-23    80s
## 7     7   Matthew Stafford 520 DET 2012-01-01    10s
## 8     8          Tom Brady 517 NWE 2011-09-12    10s
## 9     9         Jared Goff 517 LAR 2019-09-29    10s
## 10   10         Phil Simms 513 NYG 1985-10-13    80s



10. Use a loop to calculate the final balance, rounded to the nearest cent, in an account that earns 3.24% interest compounded monthly after six years if the original balance is $1,500.

principal <- 1500
rate <- .0324
freq <- 12
years <- 6

for (rolls in 1:(freq*years))
{
  principal=principal*(1+(rate/freq))
}

sprintf("%.2f",principal)
## [1] "1821.40"
  # format method perhaps not as functional as sprintf
format(principal, digits=6, nsmall=2)
## [1] "1821.40"



11. Create a numeric vector of length 20 and then write code to calculate the sum of every third element of the vector you have created.

# Jet win total for the past 20 years
vector_num1 = c(2,7,4,5,5,10,4,8,6,8,11,9,9,4,10,4,10,6,9,10)

vector_num1%%3                          # all the values of vector_num1  mod 3
##  [1] 2 1 1 2 2 1 1 2 0 2 2 0 0 1 1 1 1 0 0 1
vector_num1[1:20%%3==0]                 # the values of every 3rd number, i.e.  4 10  6  9 10  6
## [1]  4 10  6  9 10  6
sum(vector_num1[1:20%%3==0])            # 45 = 4+10+6+9+10+6
## [1] 45



  1. Use a for loop to calculate : \(\sum_{i=1}^{10} 2^i\)
x=2
sumx=0

for (i in 1:10)
{
  sumx=sumx+x^i            # series is {2,4,8,16...1024}
}
sumx                        # 2046
## [1] 2046



13. Use a while loop to accomplish the same task as in the previous exercise

x=2
sumx=0
i=1

while (i <= 10)
{
  sumx=sumx+x^i            # series is {2,4,8,16...1024}
  i=i+1
}

print(sumx)                        # 2046
## [1] 2046



14. Solve the problem from the previous two exercises without using a loop.

sumx<-sum(2^(1:10))
sumx
## [1] 2046



  1. Bonus : Work on Equations

See here for more info

Inline mathematical material is set off by the use of single dollar-sign characters. This is better to integrate text and equations.

This ($_{i=1}^n X_i$) is inline: \(\sum_{i=1}^n X_i\)

Display form uses 2 dollar signs. ( use backslash if you every want to display a dollar sign )

\[\sum_{i=1}^n X_i\]

Underline for subscript. Carot for superscript. If the expression is more than one letter,then use brackets $$prin^{1+rate}$$

\[prin^{1+rate}\]



Doesnt matter if subscript or superscript comes first $$X^2_{i,j}$$

\[X^2_{i,j}\]

$$X_{i,j}^2$$ \[X_{i,j}^2\]

Use for square root $$$$ \[\sqrt{b^2 - 4ac}\]

Displayed fractions are typeset using the operator.

$$$$ \[\frac{4z^3}{16}\]

Calculate the sum of \(2^i\) where i is 1:10

$$_{i=1}^{10} 2^i$$ \[\sum_{i=1}^{10} 2^i\]



use \left and \right to generate full size parenthesis $$_{i=1}^{n}( )$$ \[\sum_{i=1}^{n}\left( \frac{X_i}{Y_i} \right)\]

Greek letters :

\[\alpha, \beta, \gamma, \Gamma\]

\(x \ge 15\) \(x \le 15\)



n choose k \[n \choose k\]







More Notes



Notes About Latex

  1. Matrixes are achieved via begin and end for vmatrix, matrix or array
  2. italics 1 asterix
  3. bold 2 asterixes
  4. subscript tilde
  5. superscript carot

Note : Latex is but one method to format equations. MathML and MathJAX are 2 others





Notes About Font/Color Formatting

  1. Markdown and Colab both can incorprate basic html but its painful, syntax errors will not be reported !
  2. see https://www.w3schools.com/cssref/ for css reference
  3. legacy tags like <center><u><b> work but styles and css files would be better
  4. i only got the css classes working for “pre” and “body”, not for r.code
  5. also note while saving off your html is nice. Its the rmd file you want to save as that is the true source code.