1. Create a vector that contains 20 numbers. (You may choose whatever numbers you like, but make sure there are some duplicates.)
xNum<-c(1:7,c(5.5,6.8,5.5,4,2,3,5,5),10:14)
xNum 
##  [1]  1.0  2.0  3.0  4.0  5.0  6.0  7.0  5.5  6.8  5.5  4.0  2.0  3.0  5.0
## [15]  5.0 10.0 11.0 12.0 13.0 14.0
  1. Use R to convert the vector from question 1 into a character vector.
xChar<-as.character(xNum)
xChar
##  [1] "1"   "2"   "3"   "4"   "5"   "6"   "7"   "5.5" "6.8" "5.5" "4"  
## [12] "2"   "3"   "5"   "5"   "10"  "11"  "12"  "13"  "14"
  1. Use R to convert the vector from question 1 into a vector of factors.
xNum<-sort(xNum,decreasing = FALSE)
xFact<-as.factor(as.character(xNum))
xFact 
##  [1] 1   2   2   3   3   4   4   5   5   5   5.5 5.5 6   6.8 7   10  11 
## [18] 12  13  14 
## Levels: 1 10 11 12 13 14 2 3 4 5 5.5 6 6.8 7
  1. Use R to show how many levels the vector in the previous question has.
nlevels(xFact)
## [1] 14
length(unique(xFact))
## [1] 14
  1. Use R to create a vector that takes the vector from question 1 and performs on it the formula 3x^2 - 4x + 1
result<-function(x){3*x^2-4*x+1}
result(0)
## [1] 1
  1. Create a named list. That is, create a list with several elements that are each able to be referenced by name.
xList<-list(classNum=600:620,Info=c("Name","ID","E-mail"))
xList$classNum[5:10]
## [1] 604 605 606 607 608 609
xList$Info[2]
## [1] "ID"
  1. 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). #I took data from NASDAQ, http://www.nasdaq.com/markets/ipos/activity.aspx?tab=pricings&month=2017-06 #IPO in June 2017
ComQuoptes<-as.character(c("BY" ,"TNTR" ,"ALRN" ,"APRN" ,"DOVA" ,"MRSN" ,"TPGHU" ,"ESQ" ,"ATXI" ,"GPMT"))
Exchange<-c("NYSE","NASDAQ Global","NYSE","NASDAQ Global","NYSE","NASDAQ Global","NASDAQ Global Select","NYSE","NASDAQ Capital","NYSE")
Price<-as.numeric(c(19,7,15,10,17,15,10,14,6,19.50))
PubDate<-as.Date(c("2017-6-30","2017-6-30","2017-6-29","2017-6-29","2017-6-29","2017-6-28","2017-6-28","2017-6-27","2017-6-27","2017-6-23"))

df<-data.frame(ComQuoptes,Exchange,Price,PubDate)
df$ComQuoptes <- as.character(df$ComQuoptes)
str(df)
## 'data.frame':    10 obs. of  4 variables:
##  $ ComQuoptes: chr  "BY" "TNTR" "ALRN" "APRN" ...
##  $ Exchange  : Factor w/ 4 levels "NASDAQ Capital",..: 4 2 4 2 4 2 3 4 1 4
##  $ Price     : num  19 7 15 10 17 15 10 14 6 19.5
##  $ PubDate   : Date, format: "2017-06-30" "2017-06-30" ...
  1. 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.)
dfnew<-data.frame(ComQuoptes="HCACU",Exchange="NYSE MKT",Price=20,PubDate="2017-6-23")

str(dfnew)
## 'data.frame':    1 obs. of  4 variables:
##  $ ComQuoptes: Factor w/ 1 level "HCACU": 1
##  $ Exchange  : Factor w/ 1 level "NYSE MKT": 1
##  $ Price     : num 20
##  $ PubDate   : Factor w/ 1 level "2017-6-23": 1
dfnew$ComQuoptes<-as.character(dfnew$ComQuoptes)
dfnew$Exchange<-as.character(dfnew$Exchange)
dfnew$Price<-as.numeric(dfnew$Price)
dfnew$PubDate <-as.Date(dfnew$PubDate)


str(dfnew)
## 'data.frame':    1 obs. of  4 variables:
##  $ ComQuoptes: chr "HCACU"
##  $ Exchange  : chr "NYSE MKT"
##  $ Price     : num 20
##  $ PubDate   : Date, format: "2017-06-23"
dfnew$ComQuoptes
## [1] "HCACU"
str(dfnew)
## 'data.frame':    1 obs. of  4 variables:
##  $ ComQuoptes: chr "HCACU"
##  $ Exchange  : chr "NYSE MKT"
##  $ Price     : num 20
##  $ PubDate   : Date, format: "2017-06-23"
df <-rbind(df,dfnew)
df
##    ComQuoptes             Exchange Price    PubDate
## 1          BY                 NYSE  19.0 2017-06-30
## 2        TNTR        NASDAQ Global   7.0 2017-06-30
## 3        ALRN                 NYSE  15.0 2017-06-29
## 4        APRN        NASDAQ Global  10.0 2017-06-29
## 5        DOVA                 NYSE  17.0 2017-06-29
## 6        MRSN        NASDAQ Global  15.0 2017-06-28
## 7       TPGHU NASDAQ Global Select  10.0 2017-06-28
## 8         ESQ                 NYSE  14.0 2017-06-27
## 9        ATXI       NASDAQ Capital   6.0 2017-06-27
## 10       GPMT                 NYSE  19.5 2017-06-23
## 11      HCACU             NYSE MKT  20.0 2017-06-23
str(df)
## 'data.frame':    11 obs. of  4 variables:
##  $ ComQuoptes: chr  "BY" "TNTR" "ALRN" "APRN" ...
##  $ Exchange  : Factor w/ 5 levels "NASDAQ Capital",..: 4 2 4 2 4 2 3 4 1 4 ...
##  $ Price     : num  19 7 15 10 17 15 10 14 6 19.5 ...
##  $ PubDate   : Date, format: "2017-06-30" "2017-06-30" ...
  1. Show the code that would read in a CSV file called temperatures.csv from the current working directory.
myData<-read.csv(file("temperatures.csv","r"))
myData
  1. 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.
OrigiBlance<-1500
yr_Rate<-3.24
Mon_Rate<-yr_Rate/100/12
principal <- 1500
yrs<- 6

total_Mon=yrs*12
AVInterest<-1.0

for(count in 1:(yrs*12)){
  AVInterest<-AVInterest*(Mon_Rate+1)
  count<-count+1
 }
Balance<-OrigiBlance*AVInterest
sprintf("%.2f", Balance)
## [1] "1821.40"
  1. 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.
xNum<- c(12.5,33,1,203,55,1,0,54,1,11,1.98,1,2,7,1,4,17,1,35,6.45)
sumNum<-0
for(count in 1:20){
  if ((count%%3)==0){
    sumNum<-sumNum+xNum[count]
    count<-count+3
  }
  else {count<-count+1}
}
print(sumNum)
## [1] 6
  1. Use a for loop to calculate Sum of x^i where 1<=i<=10 for value x=2
x<-2
a<-0
for(n in 1:10){ 
  a<-a+x^n
}
print(a)
## [1] 2046
  1. Use a while loop to accomplish the same task as in the previous exercise.
x<-2
b<-0
n<-1
while(n < 11){ 
  b<-b+x^n
  n<-n+1
}
print(b)
## [1] 2046
  1. Solve the problem from the previous two exercises without using a loop.
x<-2
print(sum(x^(1:10)))
## [1] 2046