1) Creating a for loop that tells you what number each letter of the alphabet is.

nth <- scales::ordinal(1:26)
for (i in 1:26){
  print(paste0(letters[i], " is the ", nth[i], " letter of the alphabet."))
}
## [1] "a is the 1st letter of the alphabet."
## [1] "b is the 2nd letter of the alphabet."
## [1] "c is the 3rd letter of the alphabet."
## [1] "d is the 4th letter of the alphabet."
## [1] "e is the 5th letter of the alphabet."
## [1] "f is the 6th letter of the alphabet."
## [1] "g is the 7th letter of the alphabet."
## [1] "h is the 8th letter of the alphabet."
## [1] "i is the 9th letter of the alphabet."
## [1] "j is the 10th letter of the alphabet."
## [1] "k is the 11th letter of the alphabet."
## [1] "l is the 12th letter of the alphabet."
## [1] "m is the 13th letter of the alphabet."
## [1] "n is the 14th letter of the alphabet."
## [1] "o is the 15th letter of the alphabet."
## [1] "p is the 16th letter of the alphabet."
## [1] "q is the 17th letter of the alphabet."
## [1] "r is the 18th letter of the alphabet."
## [1] "s is the 19th letter of the alphabet."
## [1] "t is the 20th letter of the alphabet."
## [1] "u is the 21st letter of the alphabet."
## [1] "v is the 22nd letter of the alphabet."
## [1] "w is the 23rd letter of the alphabet."
## [1] "x is the 24th letter of the alphabet."
## [1] "y is the 25th letter of the alphabet."
## [1] "z is the 26th letter of the alphabet."

2) Creating a variable called count, seeing if it is divisible by 4 and removing it if it is.

count <- 1
while(count < 40)  {
  count=count + 1
  if (count %% 4==0){
  next()
  }
print(count)
}
## [1] 2
## [1] 3
## [1] 5
## [1] 6
## [1] 7
## [1] 9
## [1] 10
## [1] 11
## [1] 13
## [1] 14
## [1] 15
## [1] 17
## [1] 18
## [1] 19
## [1] 21
## [1] 22
## [1] 23
## [1] 25
## [1] 26
## [1] 27
## [1] 29
## [1] 30
## [1] 31
## [1] 33
## [1] 34
## [1] 35
## [1] 37
## [1] 38
## [1] 39

3) Using apply to get 5 number summary for mtcars.

apply(mtcars, 2, summary)
##              mpg    cyl     disp       hp     drat      wt     qsec     vs
## Min.    10.40000 4.0000  71.1000  52.0000 2.760000 1.51300 14.50000 0.0000
## 1st Qu. 15.42500 4.0000 120.8250  96.5000 3.080000 2.58125 16.89250 0.0000
## Median  19.20000 6.0000 196.3000 123.0000 3.695000 3.32500 17.71000 0.0000
## Mean    20.09062 6.1875 230.7219 146.6875 3.596563 3.21725 17.84875 0.4375
## 3rd Qu. 22.80000 8.0000 326.0000 180.0000 3.920000 3.61000 18.90000 1.0000
## Max.    33.90000 8.0000 472.0000 335.0000 4.930000 5.42400 22.90000 1.0000
##              am   gear   carb
## Min.    0.00000 3.0000 1.0000
## 1st Qu. 0.00000 3.0000 2.0000
## Median  0.00000 4.0000 2.0000
## Mean    0.40625 3.6875 2.8125
## 3rd Qu. 1.00000 4.0000 4.0000
## Max.    1.00000 5.0000 8.0000

4) Use the tapply to find the mean and standard deviation for Sepal.Length variable for each Species.

data(iris)
tapply(iris$Sepal.Length, iris$Species, mean)
##     setosa versicolor  virginica 
##      5.006      5.936      6.588
tapply(iris$Sepal.Length, iris$Species, sd)
##     setosa versicolor  virginica 
##  0.3524897  0.5161711  0.6358796

5) Use the lapply function to conduct one-sample t-tests on each of the 3 vectors in irislist.

irislist <- split(iris$Sepal.Length,iris$Species)
lapply(irislist, FUN=t.test, mu = 6, alternative = "greater")
## $setosa
## 
##  One Sample t-test
## 
## data:  X[[i]]
## t = -19.94, df = 49, p-value = 1
## alternative hypothesis: true mean is greater than 6
## 95 percent confidence interval:
##  4.922425      Inf
## sample estimates:
## mean of x 
##     5.006 
## 
## 
## $versicolor
## 
##  One Sample t-test
## 
## data:  X[[i]]
## t = -0.87674, df = 49, p-value = 0.8075
## alternative hypothesis: true mean is greater than 6
## 95 percent confidence interval:
##  5.813616      Inf
## sample estimates:
## mean of x 
##     5.936 
## 
## 
## $virginica
## 
##  One Sample t-test
## 
## data:  X[[i]]
## t = 6.5386, df = 49, p-value = 1.72e-08
## alternative hypothesis: true mean is greater than 6
## 95 percent confidence interval:
##  6.437233      Inf
## sample estimates:
## mean of x 
##     6.588

6) Solving the FizzBuzz problem.

test_vec <- c(100:150)
ifelse(test_vec %% 15==0, "FizzBuzz",
    ifelse(test_vec %% 5==0, "Buzz",
        ifelse(test_vec %% 2==0, "Fizz",
test_vec)))
##  [1] "Buzz"     "101"      "Fizz"     "103"      "Fizz"     "FizzBuzz"
##  [7] "Fizz"     "107"      "Fizz"     "109"      "Buzz"     "111"     
## [13] "Fizz"     "113"      "Fizz"     "Buzz"     "Fizz"     "117"     
## [19] "Fizz"     "119"      "FizzBuzz" "121"      "Fizz"     "123"     
## [25] "Fizz"     "Buzz"     "Fizz"     "127"      "Fizz"     "129"     
## [31] "Buzz"     "131"      "Fizz"     "133"      "Fizz"     "FizzBuzz"
## [37] "Fizz"     "137"      "Fizz"     "139"      "Buzz"     "141"     
## [43] "Fizz"     "143"      "Fizz"     "Buzz"     "Fizz"     "147"     
## [49] "Fizz"     "149"      "FizzBuzz"

7) Create a function called convert_cm_to_in that converts centimeters to inches.

convert_cm_to_in <- function(cm, inch){
    inch = cm / 2.54}
print(convert_cm_to_in(25))
## [1] 9.84252
print(convert_cm_to_in(1:100))
##   [1]  0.3937008  0.7874016  1.1811024  1.5748031  1.9685039  2.3622047
##   [7]  2.7559055  3.1496063  3.5433071  3.9370079  4.3307087  4.7244094
##  [13]  5.1181102  5.5118110  5.9055118  6.2992126  6.6929134  7.0866142
##  [19]  7.4803150  7.8740157  8.2677165  8.6614173  9.0551181  9.4488189
##  [25]  9.8425197 10.2362205 10.6299213 11.0236220 11.4173228 11.8110236
##  [31] 12.2047244 12.5984252 12.9921260 13.3858268 13.7795276 14.1732283
##  [37] 14.5669291 14.9606299 15.3543307 15.7480315 16.1417323 16.5354331
##  [43] 16.9291339 17.3228346 17.7165354 18.1102362 18.5039370 18.8976378
##  [49] 19.2913386 19.6850394 20.0787402 20.4724409 20.8661417 21.2598425
##  [55] 21.6535433 22.0472441 22.4409449 22.8346457 23.2283465 23.6220472
##  [61] 24.0157480 24.4094488 24.8031496 25.1968504 25.5905512 25.9842520
##  [67] 26.3779528 26.7716535 27.1653543 27.5590551 27.9527559 28.3464567
##  [73] 28.7401575 29.1338583 29.5275591 29.9212598 30.3149606 30.7086614
##  [79] 31.1023622 31.4960630 31.8897638 32.2834646 32.6771654 33.0708661
##  [85] 33.4645669 33.8582677 34.2519685 34.6456693 35.0393701 35.4330709
##  [91] 35.8267717 36.2204724 36.6141732 37.0078740 37.4015748 37.7952756
##  [97] 38.1889764 38.5826772 38.9763780 39.3700787

8) Add an addition input to the function that rounds the result to two decimal places by default.

convert_cm_to_in <- function(cm, inch){
    inch = cm / 2.54
    return(round(cm / 2.54,2))
}
print(convert_cm_to_in(1:100))
##   [1]  0.39  0.79  1.18  1.57  1.97  2.36  2.76  3.15  3.54  3.94  4.33  4.72
##  [13]  5.12  5.51  5.91  6.30  6.69  7.09  7.48  7.87  8.27  8.66  9.06  9.45
##  [25]  9.84 10.24 10.63 11.02 11.42 11.81 12.20 12.60 12.99 13.39 13.78 14.17
##  [37] 14.57 14.96 15.35 15.75 16.14 16.54 16.93 17.32 17.72 18.11 18.50 18.90
##  [49] 19.29 19.69 20.08 20.47 20.87 21.26 21.65 22.05 22.44 22.83 23.23 23.62
##  [61] 24.02 24.41 24.80 25.20 25.59 25.98 26.38 26.77 27.17 27.56 27.95 28.35
##  [73] 28.74 29.13 29.53 29.92 30.31 30.71 31.10 31.50 31.89 32.28 32.68 33.07
##  [85] 33.46 33.86 34.25 34.65 35.04 35.43 35.83 36.22 36.61 37.01 37.40 37.80
##  [97] 38.19 38.58 38.98 39.37

9) Create a function called count_my_change that adds up the total amount of money you input.

count_my_change <- function(dollars=0, quarters=0, dimes=0, nickels=0, pennies=0){
     total <- c(0.25*quarters + 0.01*pennies + 0.10*dimes + 0.05*nickels + 1.00*dollars)
    return("Your total is $amount")
}
print(count_my_change(dollars = 5))
## [1] "Your total is $amount"
print(count_my_change(dimes = 2))
## [1] "Your total is $amount"
print(count_my_change(quarters = 3))
## [1] "Your total is $amount"
print(count_my_change(pennies = 4))
## [1] "Your total is $amount"
print(count_my_change(nickels = 1))
## [1] "Your total is $amount"

Without input, the total amount is $1.41

with 5 dollars, the total amount is $5.41

With 2 dimes, the total amount is $1.51

With 3 quarters, the total amount is $1.91

With 4 pennies, the total amount is $1.42

With 1 nickel, the total amount is $1.41