# 1. Create a vector that contains 20 numbers_______________________Q1
v <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
# 2. Convert vector objects to character type_______________________Q2
vchar <- as.character(v)
# Verify conversion
is.character(vchar)
## [1] TRUE
vchar
## [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14"
## [15] "15" "16" "17" "18" "19" "20"
# 3. Convert vector abjects to factor type__________________________Q3
vfact <- as.factor(v)
# verify converstion
is.factor(vfact)
## [1] TRUE
vfact
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
## Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# 4. Count number of levels in vector of factors____________________Q4
nlevels(vfact)
## [1] 20
# 5. Perform quadratic function on vector___________________________Q5
# create / assign function
quad <- function(v) {1 + 4*v + (3*v)^2}
# apply function
vquad <- quad(v)
vquad
## [1] 14 45 94 161 246 349 470 609 766 941 1134 1345 1574 1821
## [15] 2086 2369 2670 2989 3326 3681
# 6. Create named list______________________________________________Q6
nlist <- list(foo = c(99, 98, 97), bar = c("hello", "world"))
# verify names can be accessed
nlist$foo[2]
## [1] 98
nlist$bar[1]
## [1] "hello"
# 7. Create dataframe with four columns_____________________________Q7
# character column
joke <- c("Some", "people", "can", "extrapolate", "but", "others",
"cannot", "finish", "this", "joke")
# factor column
pos <- c("adjective", "noun", "verb", "verb", "conjunction",
"adjective", "verb", "verb", "preposition", "noun")
# numeric column
bers <- c(1,2,3,4,5,6,7,8,9,10)
# date column
dates <- seq(as.Date("1973/10/01"), by="day", length=10)
# merge columns to create dataframe
df1 <- data.frame(joke, pos, bers, dates)
# make sure dataframe looks right
df1
## joke pos bers dates
## 1 Some adjective 1 1973-10-01
## 2 people noun 2 1973-10-02
## 3 can verb 3 1973-10-03
## 4 extrapolate verb 4 1973-10-04
## 5 but conjunction 5 1973-10-05
## 6 others adjective 6 1973-10-06
## 7 cannot verb 7 1973-10-07
## 8 finish verb 8 1973-10-08
## 9 this preposition 9 1973-10-09
## 10 joke noun 10 1973-10-10
# 8. Illustrate how to add a row with a new factor__________________Q8
### note: I'm not sure how to illustrate, other than to do it.
# create new row
newRow <- data.frame(joke="HA!",pos="interjection",bers=11,
dates=(as.Date("1973/10/23")))
# add it to dataframe
df1 <- rbind(df1,newRow)
# verify dataframe has been updated
tail(df1)
## joke pos bers dates
## 6 others adjective 6 1973-10-06
## 7 cannot verb 7 1973-10-07
## 8 finish verb 8 1973-10-08
## 9 this preposition 9 1973-10-09
## 10 joke noun 10 1973-10-10
## 11 HA! interjection 11 1973-10-23
# 9. Show the code that would read in a CSV file____________________Q9
# commenting this out so that it doesn't come back with an error
# temps <- read.csv("temperatures.csv")
# 10. Use a loop to calculate the final balance____________________Q10
#assign variables for balance, interest rate, term, and month
# note: I saw that in your solution you handled the interest rate as
# an APR. I hadn't done that because the problem had only said that
# it earned that interest rate compounded monthly. Real world I
# would have clarified with client or checked docs.
balance <- 1500
interest <- 0.0324
term <- 80
month <- 1
# apply / loop function
while(month<term){
balance <- balance * (1 + interest)
month <- month + 1
}
# check result
balance
## [1] 18624.43
# 11. Create a numeric vector and calculate every 3rd number________Q11
# creating 20 digit vector
v <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
# create vector consisting only of every 3rd number
v3rd <- v[seq(3, length(v), 3)]
# verify 2nd vector is what it should be
v3rd
## [1] 3 6 9 12 15 18
# get sum of all items in the second vector
sum(v3rd)
## [1] 63
# 12. for loop calculation_________________________________________Q12
# set output to zero
output_for <- 0
# create for loop: calculate and sum the exponential result of 2 to
# the power of each number from 1-10 (e.g., 2^1+2^2+2^3...)
for(i in 1:10){
output_for <- output_for + (2^i)
}
# check result
output_for
## [1] 2046
# 13. while loop calculation_______________________________________Q13
# set output to zero and set exponent to 1
output_while <- 0
exponent <- 1
# create while loop: calculate and sum the result of 2 to the power
# of each exponent while that number is less than 10
while(exponent < 11){
output_while <- output_while + 2^exponent
exponent <- exponent+1}
# check result
output_while
## [1] 2046
# 13. calculate w/o loop___________________________________________Q14
output_straight <- 2^1+2^2+2^3+2^4+2^5+2^6+2^7+2^8+2^9+2^10
output_straight
## [1] 2046