The purpose of this document is to review the hands on lab quiz and practice R exercices in Week-1.
Question 1: Create a vector that contains 20 numbers.
number_vector <- c (1,2,3,4,5,1,2,3,4,5,6,7,8,9,10,11,12,13,13,12)
length(number_vector)
## [1] 20
number_vector
## [1] 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 13 12
Question 2: Use R to convert vector from question 1 into a character vector.
number_vector <- as.character(number_vector)
typeof(number_vector)
## [1] "character"
Question 3: Use R to convert vector from question 1 into a vector of factors:
number_vector <- as.factor(number_vector)
typeof(number_vector)
## [1] "integer"
class(number_vector)
## [1] "factor"
Question 4: Use R to show how many levels the vector in the previous has.
nlevels(number_vector)
## [1] 13
Question 5: Use R to create a vector that takes vector from question 1 and performs on it the formula 3x2 -4x+1
Please note: In this case i realized i already converted the vector in question 1 into a factor type so i will create another numeric vector to calculate the equation.
new_vector <- c(1:20)
new_vector
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
new_vector_2 <- 3*new_vector^2-4*new_vector+1
new_vector_2
## [1] 0 5 16 33 56 85 120 161 208 261 320 385 456 533
## [15] 616 705 800 901 1008 1121
Question 6: Create a named list that has several elements that are each able to be referenced by name:
new_list <- list(title=c('Toy Story', 'Star Wars', 'Guardians of the Galaxy', 'Spiderman'), ratings=c(3,4,3,2))
new_list
## $title
## [1] "Toy Story" "Star Wars"
## [3] "Guardians of the Galaxy" "Spiderman"
##
## $ratings
## [1] 3 4 3 2
Question 7: Create a dataframe with 4 columns - one each character, 3 factor levels, numeric and date. with 10 rows.
title <- as.character(c('Toy Story', 'Star Wars', 'Guardians of the Galaxy', 'Spiderman', 'Incredibles 2',
'Venom', 'Black Panther', 'Avengers', 'Iron Man', 'Avatar'))
release <- as.Date(c('2008-1-1', '2019-1-1', '2018-1-1', '2014-1-1', '1982-1-1',
'2007-1-1', '2015-1-1', '2016-1-1', '2019-1-1', '2017-1-1'))
genre <- c('comedy', 'adventure', 'action', 'comedy', 'adventure',
'action', 'comedy', 'adventure', 'action', 'comedy')
ratings <- as.numeric(c(1,2,3,4,5,6,7,8,9,10))
df <- data.frame(title, release, genre, ratings)
head(df)
## title release genre ratings
## 1 Toy Story 2008-01-01 comedy 1
## 2 Star Wars 2019-01-01 adventure 2
## 3 Guardians of the Galaxy 2018-01-01 action 3
## 4 Spiderman 2014-01-01 comedy 4
## 5 Incredibles 2 1982-01-01 adventure 5
## 6 Venom 2007-01-01 action 6
str(df)
## 'data.frame': 10 obs. of 4 variables:
## $ title : Factor w/ 10 levels "Avatar","Avengers",..: 9 8 4 7 5 10 3 2 6 1
## $ release: Date, format: "2008-01-01" "2019-01-01" ...
## $ genre : Factor w/ 3 levels "action","adventure",..: 3 2 1 3 2 1 3 2 1 3
## $ ratings: num 1 2 3 4 5 6 7 8 9 10
# i see title has 10 factors. i need to make that a character.
df$title <- as.character(df$title)
str(df)
## 'data.frame': 10 obs. of 4 variables:
## $ title : chr "Toy Story" "Star Wars" "Guardians of the Galaxy" "Spiderman" ...
## $ release: Date, format: "2008-01-01" "2019-01-01" ...
## $ genre : Factor w/ 3 levels "action","adventure",..: 3 2 1 3 2 1 3 2 1 3
## $ ratings: num 1 2 3 4 5 6 7 8 9 10
Question 8: Illustrate how to add a row with a value for the factor column that isnt already in the list.
new_movie <- data.frame(title='Batman', release='1996-1-1', genre='drama', ratings=10)
new_movie
## title release genre ratings
## 1 Batman 1996-1-1 drama 10
new_movie$title <- as.character(new_movie$title)
new_movie$release <- as.Date(new_movie$release)
df <- rbind(df, new_movie)
df
## title release genre ratings
## 1 Toy Story 2008-01-01 comedy 1
## 2 Star Wars 2019-01-01 adventure 2
## 3 Guardians of the Galaxy 2018-01-01 action 3
## 4 Spiderman 2014-01-01 comedy 4
## 5 Incredibles 2 1982-01-01 adventure 5
## 6 Venom 2007-01-01 action 6
## 7 Black Panther 2015-01-01 comedy 7
## 8 Avengers 2016-01-01 adventure 8
## 9 Iron Man 2019-01-01 action 9
## 10 Avatar 2017-01-01 comedy 10
## 11 Batman 1996-01-01 drama 10
Question 9: Show the code that would read in a CSV file called temperatures.csv
Please note; there will not be an output of the CSV file as i do not have the csv in my local. instead i used movies-db.csv
df_new <- read.csv('movies-db.csv', header=TRUE, sep=',')
df_new
## name year length_min genre
## 1 Toy Story 1995 81 Animation
## 2 Akira 1998 125 Animation
## 3 The Breakfast Club 1985 97 Drama
## 4 The Artist 2011 100 Romance
## 5 Modern Times 1936 87 Comedy
## 6 Fight Club 1999 139 Drama
## 7 City of God 2002 130 Crime
## 8 The Untouchables 1987 119 Drama
## 9 Star Wars Episode IV 1977 121 Action
## 10 American Beauty 1999 122 Drama
## 11 Room 2015 118 Drama
## 12 Dr. Strangelove 1964 94 Comedy
## 13 The Ring 1998 95 Horror
## 14 Monty Python and the Holy Grail 1975 91 Comedy
## 15 High School Musical 2006 98 Comedy
## 16 Shaun of the Dead 2004 99 Horror
## 17 Taxi Driver 1976 113 Crime
## 18 The Shawshank Redemption 1994 142 Crime
## 19 Interstellar 2014 169 Adventure
## 20 Casino 1995 178 Biography
## 21 The Goodfellas 1990 145 Biography
## 22 Blue is the Warmest Colour 2013 179 Romance
## 23 Black Swan 2010 108 Thriller
## 24 Back to the Future 1985 116 Sci-fi
## 25 The Wave 2008 107 Thriller
## 26 Whiplash 2014 106 Drama
## 27 The Grand Hotel Budapest 2014 100 Crime
## 28 Jumanji 1995 104 Fantasy
## 29 The Eternal Sunshine of the Spotless Mind 2004 108 Drama
## 30 Chicago 2002 113 Comedy
## average_rating cost_millions foreign age_restriction
## 1 8.3 30.0 0 0
## 2 8.1 10.4 1 14
## 3 7.9 1.0 0 14
## 4 8.0 15.0 1 12
## 5 8.6 1.5 0 10
## 6 8.9 63.0 0 18
## 7 8.7 3.3 1 18
## 8 7.9 25.0 0 14
## 9 8.7 11.0 0 10
## 10 8.4 15.0 0 14
## 11 8.3 13.0 1 14
## 12 8.5 1.8 1 10
## 13 7.3 1.2 1 18
## 14 8.3 0.4 1 18
## 15 5.2 4.2 0 0
## 16 8.0 6.1 1 18
## 17 8.3 1.3 1 14
## 18 9.3 25.0 0 16
## 19 8.6 165.0 0 10
## 20 8.2 50.0 0 18
## 21 8.7 25.0 0 14
## 22 7.8 4.5 1 18
## 23 8.0 13.0 0 16
## 24 8.5 19.0 0 0
## 25 7.6 5.5 1 16
## 26 8.5 3.3 1 12
## 27 8.1 25.5 0 14
## 28 6.9 65.0 0 12
## 29 8.3 20.0 0 14
## 30 7.2 45.0 0 12
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 $1500. Compount interes every month. 12 months in a year. period per year is 12. principal is $1500 we are trying to calculate the final balance after 6 years. interest rate is 3.25% which is 0.0325
–
Question 11:Create a numeric vector length 20 and then write a code to calculate the sum of every third element of the vector you have created. To get the every 3rd element i can use [… %%3]
num_vector <- c(1:20)
sum(num_vector[num_vector %% 3])
## [1] 21
Question 12: Use loop to calculate (could not add special characters) for the value x=2 Basically the question: get sum of x^1 + x ^2 + x^3 …. up to x^10 for value x being 2 –
Question 13: Use a while loop to accomplish the same task as in question 12.
–
Question 14: Solve the problem freom the two previous exercises without using a loop.
x <- 2
sum(x^(1:10))
## [1] 2046