1. Create a vector that contains 20 numbers:

data.set <- c(5:13, 10:15, 3:6, 20)
data.set
##  [1]  5  6  7  8  9 10 11 12 13 10 11 12 13 14 15  3  4  5  6 20
length(data.set)
## [1] 20

2. Convert the vector from 1 into a character vector:

character.data.set <- as.character(data.set)
is.character(character.data.set)
## [1] TRUE

3. Convert the vector from 1 into a vector of factors:

factor.data.set <- as.factor(data.set)
is.factor(factor.data.set)
## [1] TRUE

4. Show how many levels the vector in the previous:

factor.data.set
##  [1] 5  6  7  8  9  10 11 12 13 10 11 12 13 14 15 3  4  5  6  20
## Levels: 3 4 5 6 7 8 9 10 11 12 13 14 15 20

5. Vector that takes the vector from 1 and performs on it the formula 3x2 - 4x + 1:

formula.data.set <- ((3*data.set ^ 2 ) - (4*data.set) + 1)
formula.data.set
##  [1]   56   85  120  161  208  261  320  385  456  261  320  385  456  533  616
## [16]   16   33   56   85 1121

6. Create a named list. That is, create a list with several elements that are each able to be referenced by name:

nfl.win.count.list <- c("Patriot" = 5, "Jets" = 3, "Bills" = 4, "Dolphins" = 4)
nfl.win.count.list["Bills"]
## Bills 
##     4

7. Create a data frame with four columns – one each character, factor (with three levels), numeric, and date. Your data frame should have at least 10 observations (rows):

id.numeric.list <- c(1:10)
played.date.list <- rep( as.Date("2019-12-20") , 10)
grade.list <- c("A","B","A+", "B", "B", "A","A+","B","A","B")
grade.factor.list <-  as.factor(grade.list)
name.char.list <- c("Hockey", "Football", "Baseball", "Curling", "Rugby", "Lacrosse", "Basketball", "Tennis", "Cricket", "Soccer") 

theDF.data.set <- data.frame (id = id.numeric.list, Name = name.char.list, Grade =grade.factor.list ,PlayedOn =played.date.list, stringsAsFactors=FALSE)

theDF.data.set
##    id       Name Grade   PlayedOn
## 1   1     Hockey     A 2019-12-20
## 2   2   Football     B 2019-12-20
## 3   3   Baseball    A+ 2019-12-20
## 4   4    Curling     B 2019-12-20
## 5   5      Rugby     B 2019-12-20
## 6   6   Lacrosse     A 2019-12-20
## 7   7 Basketball    A+ 2019-12-20
## 8   8     Tennis     B 2019-12-20
## 9   9    Cricket     A 2019-12-20
## 10 10     Soccer     B 2019-12-20
nrow(theDF.data.set)
## [1] 10

8. Illustrate how to add a row with a value for the factor column that isn’t already in the list of levels. (Note: You do not need to accomplish this with a single line of code.):

levels(theDF.data.set$Grade) <- c(levels(theDF.data.set$Grade), "A-")

theDF.data.set[nrow(theDF.data.set)+1, ]  <- list(id=11,Name="Squash", Grade ="A-", PlayedOn= as.Date("2019-12-21"))

theDF.data.set
##    id       Name Grade   PlayedOn
## 1   1     Hockey     A 2019-12-20
## 2   2   Football     B 2019-12-20
## 3   3   Baseball    A+ 2019-12-20
## 4   4    Curling     B 2019-12-20
## 5   5      Rugby     B 2019-12-20
## 6   6   Lacrosse     A 2019-12-20
## 7   7 Basketball    A+ 2019-12-20
## 8   8     Tennis     B 2019-12-20
## 9   9    Cricket     A 2019-12-20
## 10 10     Soccer     B 2019-12-20
## 11 11     Squash    A- 2019-12-21
nrow(theDF.data.set)
## [1] 11

9. Show the code that would read in a CSV file called temperatures.csv from the current working directory

getwd()
## [1] "C:/Users/kamat/OneDrive/Documents/Vinayak/CUNY/BridgeWorkshopR"
temperature.data <-read.table(file="temperatures.csv", header=TRUE, sep=",")
temperature.data
##          City Temperature
## 1      Jersey        34.5
## 2    Brooklyn        35.0
## 3 Jersey City        33.5
## 4    Stamford        28.0

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 :

get.final.balance <- function(amt, rate, frequency, duration)
{
   return (round( amt * (1 + ((rate / 100)/frequency) ) ^ (frequency * duration ) , digits=2))
}

get.final.balance(amt=1500, rate=3.24, frequency= 12 , duration=6)
## [1] 1821.4

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:

num_vector <- c(1:20)
i <- 1
sum_third <- 0
while(i <= length(num_vector))
{
  if ((i %% 3) == 0)
  {
    sum_third <- sum_third + num_vector[i]
  }
  i <- i + 1
}
num_vector
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
sum_third
## [1] 63

12. Use a for loop to calculate ∑ 𝑥𝑥 10 𝑖𝑖𝑖𝑖=1 for the value 𝑥𝑥 = 2.

final.val <- 0
x <- 2
for (i in 1:10) 
  {
    final.val <- final.val  + ( x ^ i )
  }
final.val
## [1] 2046

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

final.while.val <- 0
x <- 2
i <- 1
while ( i <= 10 ) 
  {
    final.while.val <- final.while.val  + ( x ^ i )
    i <- i+1
  }
final.while.val
## [1] 2046

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

loop.num.list <- c(1:10)
x <- 2
val <- 2 ^ loop.num.list
sum(val)
## [1] 2046