<- c("politics", "philosophy", "literature", "chemistry")
list_fields <- list_fields[list_fields!="literature"] new_list
Instructions
- Download the *.qmd file from here
- Place it in the relevant folder “week2”
- Open the *.qmd file
Question 1
Extract the word “literature” from the following vector and create a new object
<- list_fields[list_fields!="chemistry"]
new_list new_list
[1] "politics" "philosophy" "literature"
<- list_fields[list_fields=="philosophy"]
new_list new_list
[1] "philosophy"
<- list_fields[list_fields=="philosophy" | list_fields=="chemistry"]
new_list new_list
[1] "philosophy" "chemistry"
length(new_list)
[1] 2
Question 2
Remove from the following list all the numbers between 13 and 15 (inclusive)
<-c(11, 12, 13, 14, 15, 16, 17)
list_no<- list_no[list_no <= 12 | list_no >= 16]
new_list_no new_list_no
[1] 11 12 16 17
<- list_no[list_no != 13 & list_no != 14 & list_no != 15]
new_list_no2 new_list_no2
[1] 11 12 16 17
<- list_no[!(list_no >= 13 & list_no <= 15)]
filtered_list filtered_list
[1] 11 12 16 17
Question 3
Remove from the following list, “word”, “sentence”, “books” using the %in% operator.
<-c("random", "word", "sentence", "books") list_words
Question 4
Explain in your words: Why does the following “four” > “five” return TRUE
"four" > "five"
[1] TRUE
Question 5
Remove the missing data from the following list
<- c(1, 2, 3, 4, NA, 5, 6) list_no
Question 6
Calculate the mean and median from the following list of numbers
<-c(30, 12, NA, 14, NA) list_no
Question 7
Create the following dataframe
<- data.frame(student=c('Alex', 'Jane', 'Tom', 'Lilly', 'Turner', 'Ruby', 'Nick'),
df grade=c(77, 81, 89, 83, 99, 92, 97))
df
Question 8
Create a new dataframe with only Alex, Jane and Turner
Question 9
What is the average grade for the entire class
Question 10
What is the mean of Alex, Jane, and Turner’s grades
Question 11
What is the highest grade?
Question 12
What is the lowest grade?
Question 13
Who is student with the highest grade?
Question 14
Who is the student with the lowest grade?
Question 15
Extract the second element from the following list
<-c("el1", "el2", "el3") list_new
Question 16
Extract the last element from the following list
<-c("el1", "el2", "el3") list_new
Question 17
Merging Datsets
- Load the life_expectancy and urbanization datasets
#Removing previous datasets in memory
rm(list = ls())
#Setting path
setwd("/Users/bgpopescu/Dropbox/john_cabot/teaching/stats/week2/lab")
<- read.csv(file = './data/life-expectancy.csv')
life_expectancy_df <- read.csv(file = './data/share-of-population-urban.csv') urbanization_df
- Calculating mean by country for life_expectancy
- Remove the countries which have missing continents
- Calculating mean by country for urbanization
- Remove the countries which have missing continents
- Perform a left merge
- Removing NA values
- Creating a scatterplot