Hands-on Quiz Week 1
Question 1 : Create a vector that contains 20.(You may chose
whatever numbers you like, but make sure there are some duplicates)
Answer :
vec <- c(1:7, 5:13, 12:15)
vec
## [1] 1 2 3 4 5 6 7 5 6 7 8 9 10 11 12 13 12 13 14 15
Question 2 : Use R to convert the vector from question 1 into a
character vector
Answer :
vec_char <- as.character(vec)
vec_char
## [1] "1" "2" "3" "4" "5" "6" "7" "5" "6" "7" "8" "9" "10" "11" "12"
## [16] "13" "12" "13" "14" "15"
class(vec_char)
## [1] "character"
Question 3 : Use R to convert the vector from question 1 into a
vector of factors.
Answer :
vec_factor <- as.factor(vec)
vec_factor
## [1] 1 2 3 4 5 6 7 5 6 7 8 9 10 11 12 13 12 13 14 15
## Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class(vec_factor)
## [1] "factor"
Question 4 : Use R to show how many levels the vector in the
previous question has.
Answer :
nlevels(vec_factor)
## [1] 15
Question 5 : Use R to create a vector that takes the vector from
question 1 and performs on it the formula 3π₯π₯2 β 4π₯π₯ + 1.
Answer :
vec <- 3 * vec ^ 2 - 4 * vec + 1
vec
## [1] 0 5 16 33 56 85 120 56 85 120 161 208 261 320 385 456 385 456 533
## [20] 616
Question 6 : Create a named list. That is, create a list with
several elements that are each able to be referenced by name
Answer :
list_named <- list(TheInt=1:9, TheChar=c("Umer", "farooq", "MSDS"))
list_named$TheChar
## [1] "Umer" "farooq" "MSDS"
list_named$TheInt
## [1] 1 2 3 4 5 6 7 8 9
Question 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).
Answer :
name <- as.character(c("umer","amina","arham","zavi","semee","sidra","sundas","baba","ami","arsh"))
gender <- c("male","female","male","male","female","female","female","male","female","male")
age <- as.numeric(30,28,3,2,38,35,28.70,65,17)
DoB <- as.Date(c("1992-07-22","1994-07-04","2019-09-09","2021-07-20","1985-01-05","1989-09-09","1994-04-04","1952-01-01","1957-01-01","2004-01-01"))
family <- data.frame(name, gender, age, DoB)
str(family)
## 'data.frame': 10 obs. of 4 variables:
## $ name : chr "umer" "amina" "arham" "zavi" ...
## $ gender: chr "male" "female" "male" "male" ...
## $ age : num 30 30 30 30 30 30 30 30 30 30
## $ DoB : Date, format: "1992-07-22" "1994-07-04" ...
Question 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.)
Answer :
new_row <- data.frame(name="zimal", gender= "female", age=13, DoB="2008-12-12")
str(new_row)
## 'data.frame': 1 obs. of 4 variables:
## $ name : chr "zimal"
## $ gender: chr "female"
## $ age : num 13
## $ DoB : chr "2008-12-12"
family <- rbind.data.frame(family, new_row)
str(family)
## 'data.frame': 11 obs. of 4 variables:
## $ name : chr "umer" "amina" "arham" "zavi" ...
## $ gender: chr "male" "female" "male" "male" ...
## $ age : num 30 30 30 30 30 30 30 30 30 30 ...
## $ DoB : Date, format: "1992-07-22" "1994-07-04" ...
View(family)
Question 9 : Show the code that would read in a CSV file called
temperatures.csv from the current working directory
Answer :
#df <- read.csv(file = "temperatures.csv")
#df
Question 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,
Answer :
principal <- 1500
interest_rate <- .0324
interest_applied_per_period <- 12
number_of_period <-6
Final_ammount <- principal*(1+(interest_rate/interest_applied_per_period))^(interest_applied_per_period*number_of_period)
Final_ammount
## [1] 1821.396
Question 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.
Answer :
vectr <- 1:20
sum(vectr[vectr %% 3 == 0])
## [1] 63
Question 12 : Use a for loop to calculate β π₯π₯ 10 ππππ=1 for the
value π₯π₯ = 2.
Answer :
x <- 2
ans <- 0
for (i in 1:10) {
ans = ans+x^i
}
print(ans)
## [1] 2046
Question 13 : Use a while loop to accomplish the same task as in the
previous exercise.
Answer :
x <- 2
sum <- 0
i <- 1
while (i<=10) {
sum <- sum + x ^ i
i <- i+1
}
print(sum)
## [1] 2046
Question 14 : Solve the problem from the previous two exercises
without using a loop.
Answer :
x <- 2
print(sum(x^(1:10)))
## [1] 2046