Uploaded by Jose Flores

Assignment12

advertisement
Concepts and methods (FA 2020_EGN1007C ENGR)
Mehran Homayounfar - Due on Thursday, 11/19/2020
Assignment 12 (100 points)
Submission instruction:
Please watch the Week12-videos before doing assignment.
Please use the file “Assignment12.R”, uploaded on Canvas, to answer the following
questions. This is the file that you need to upload on Canvas.
Where the questions ask for a written answer (not code), also write this in your script,
with # so it is a comment.
Use spacing and comments for increasing readability.
-------------------------------------------------------------------------------------------------------------------1- Create a vector called lacroix_flavors with the elements "coconut",
"cran-raspberry", "lemon", "lime", "pomme baya", "apricot". For the
next exercises (a-c), use indexing (do not recreate vectors by using c() and
adding content!)
a) Create a vector first_order with only the first 3 elements from
lacroix_flavors. (1 pts)
b) Create a vector second_order with "lemon", "lime", "apricot",
"apricot" (in this order, and again, use indexing on lacroix_flavors).
(1 pts)
c) Create a vector third_order with only the last two elements of
lacroix_flavors, and in this case, use indexing with a minus sign. (3
pts)
2- Load the file penguin_data.csv using read_csv() (load the library in the
correct place!). Let’s say that we are only interested in data for Adelie penguins
on the island Torgersen. From looking at the data quickly, it seems like these are
only the first 19 rows. Use indexing to create a new data frame called
Adelie_Torgersen with only the first 19 rows from the original data frame, and
all columns. Do not use relational operators (==, >, <, <=, >=, != ) and logical operators (&
and | ). (10 pts). You can use an structure like: data <- data_frame[1:N,]
1
3- We have the feeling that maybe we did not actually get all the data that we
needed, so we decide to use relational and logical operators. Create the data
frame Adelie_Torgersen_base and use relational operators and logical
operators to get the rows with penguins of the species Adelie AND that are
from the island Torgersen (keep all columns). Do not use tidyverse functions or
the subset() function. (10 pts)
4- Create a logical vector showing which elements in the vector
penguin_info$body_mass_g are smaller than 3500. Use this expression to
create the data frame small_penguins with only penguin data for penguins that weigh
less than 3500 grams (keep all columns). Do not use tidyverse functions or the
subset() function. (10 pts)
2
5- Using ggplot(), make a scatter plot with body mass on the x-axis and flipper
length on the y-axis, using the data frame penguin_info. Make a similar plot
using the data frame small_penguins. Make sure that all axis labels and plot
titles are the same as the output shown! These are two separate plots. You do
not have to save the plots. (10 pts)
3
6- Complete the if-statement below so that height_ft is set to 14 if building is
equal to “house”. (5 pts)
building <- "house"
if (){
}
height_ft
7- Complete the if-statement below so that if building is equal to “house” it sets
height_ft <- 14 and if building is “skyscraper” it sets height_ft <- 500.
(10 pts)
building <- "skyscraper"
if (){
}
height_ft
8- Complete the if-statement below so that if building is equal to “house” it sets
height_ft <- 14 and if building is “skyscraper” it sets height_ft <- 500,
and if building is anything else it sets height_ft <- NA. (10 pts)
building <- "chicken coop"
if (){
}
height_ft
9- Complete the code below so that if age is larger than 12 and smaller than 20, it
returns life_stage <- "teenager"; and life_stage <- "not a teenager"
if age is any other value. (10 pts)
age <- 26
if(){
4
}
life_stage
10- Write an if-statement that creates two variables, and does it in the following way:
• If age is larger than 11 and smaller than 13, return “tween” for
adolescent_stage and “middle school” for school
• If age is larger than or equal to 13 and smaller than 15, return
“teenager” for adolescent_stage and “middle school” for school.
• If age is larger than or equal to 15 and smaller than 20, return
“teenager” for adolescent_stage and “high school” for school.
(10 pts)
Copy this code to work in:
age <- 13
if(){
}
adolescent_stage
school
11- The following vector is available in the assignment11.R script:
vector_NA <- c(12, 36, 45, 89, 15, NA, 23, 88, 55, 32, 78, 46,
81, 37, 68, NA, 91, 101, 7, 64, NA, 77, 14, 45)
Create two new vectors that have the NAs removed, using indexing and
functions:
• For vector_fixed1, use indexing and complete.cases()
• For vector_fixed2, use indexing and !is.na()
(10 pts)
5
Download