1. Create a vector that contains 20 numbers. (You may choose whatever numbers you like, but make sure there are some duplicates.)

numVec <- c(1:10, 10, 7:15)
numVec
##  [1]  1  2  3  4  5  6  7  8  9 10 10  7  8  9 10 11 12 13 14 15

2. Use R to convert the vector from question 1 into a character vector.

charVec <- as.character(numVec)
charVec
##  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "10" "7"  "8"  "9" 
## [15] "10" "11" "12" "13" "14" "15"

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

facVec <- as.factor(numVec)
facVec
##  [1] 1  2  3  4  5  6  7  8  9  10 10 7  8  9  10 11 12 13 14 15
## Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

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

nlevels(facVec)
## [1] 15
#alternative solution
length(unique(facVec))
## [1] 15

5. Use R to create a vector that takes the vector from question 1 and performs the formula 3x^2 - 4x + 1.

numVec2 <- 3 * numVec ^ 2 - 4 * numVec + 1
numVec2
##  [1]   0   5  16  33  56  85 120 161 208 261 261 120 161 208 261 320 385
## [18] 456 533 616

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

namedList <- list(scores = 1:8, days = c("monday", "tuesday", "thursday", "friday"))
namedList
## $scores
## [1] 1 2 3 4 5 6 7 8
## 
## $days
## [1] "monday"   "tuesday"  "thursday" "friday"

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

#Create different fields to be columns of data frame
people <- c("jane", "joseph", "carlos", "juan", "rachel", "manny", "anthony", "caroline", "christian", "erica")
favColor <- c("purple", "red", "blue", "purple", "red", "blue", "purple", "red", "blue", "purple")
age <- as.numeric(c(27:36))
birthDate <- as.Date(c("1992-01-01", "1991-01-01", "1990-01-01", "1989-01-01", "1988-01-01", "1987-01-01", "1986-01-01", "1985-01-01", "1984-01-01", "1983-01-01"))

#Create data frame
peopleInfo <- data.frame(people, favColor, age, birthDate)

#Convert people column to character
peopleInfo$people <- as.character(peopleInfo$people)

#Check columns are correct data type
str(peopleInfo)
## 'data.frame':    10 obs. of  4 variables:
##  $ people   : chr  "jane" "joseph" "carlos" "juan" ...
##  $ favColor : Factor w/ 3 levels "blue","purple",..: 2 3 1 2 3 1 2 3 1 2
##  $ age      : num  27 28 29 30 31 32 33 34 35 36
##  $ birthDate: Date, format: "1992-01-01" "1991-01-01" ...

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.)

#Bind new row to the peopleInfo data frame
peopleInfo <- rbind(peopleInfo, data.frame(people = as.character("jeniffer"), favColor = "green", age = as.numeric(5), birthDate = as.Date("2014-01-01")))

#Check columns are correct data type
str(peopleInfo)
## 'data.frame':    11 obs. of  4 variables:
##  $ people   : chr  "jane" "joseph" "carlos" "juan" ...
##  $ favColor : Factor w/ 4 levels "blue","purple",..: 2 3 1 2 3 1 2 3 1 2 ...
##  $ age      : num  27 28 29 30 31 32 33 34 35 36 ...
##  $ birthDate: Date, format: "1992-01-01" "1991-01-01" ...

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

#File does not exist. This is sample code.
#temperatureData <- read.csv(file = "temperatures/csv")

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

balance <- 1500
annualInterestRate <- 0.0324
compoundsPerYear <- 12
years <- 6
totalcompounds <- compoundsPerYear * years

for (i in 1:totalcompounds) {
  balance <- balance + ((balance * annualInterestRate) / compoundsPerYear)
}
sprintf("$%.2f", balance)
## [1] "$1821.40"

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

numVec3 <- c(1:20)
sumThird <- 0

for (i in 1:length(numVec3)) {
  if (i %% 3 == 0) {
    sumThird <- sumThird + numVec3[i]
  }
}
sumThird
## [1] 63

12. Use a for loop to calculate the Riemman sum of x^i from i = 1 to 10 for the value x = 2.

x <- 2
riemmanSum <- 0

for (i in 1:10) {
  riemmanSum <- riemmanSum + x ^ i
}
riemmanSum
## [1] 2046

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

x2 <- 2
riemmanSum2 <- 0
i <- 1

while (i <= 10) {
  riemmanSum2 <- riemmanSum2 + (x2 ^ i)
  i <- i + 1
}
riemmanSum2
## [1] 2046

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

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