Use R comments (lines starting with the # symbol) to clearly indicate for which
ID: 3771674 • Letter: U
Question
Use R comments (lines starting with the # symbol) to clearly indicate for which numbered item the statements are intended. A template file titled “COS-206_Written_Assignment_Template.R” can be found in the Course Documents folder.
1. Using the colon operator : create a vector called numbers1 that has the items 3, 4, 5, 6, & 7.
2. Using the Write an R script that contains R statements to address the following numbered items. c function, create a vector called numbers2 that has the items 3, 9, 27, 81, & 105.
3. Calculate the element-wise products of numbers1 and numbers2.
a. For example, the first element-wise product is the first item of numbers1 (3) times the first item of numbers2 (3).
4. Calculate the remainder for each of the item in numbers2 divided by 4.
5. What is the value of num1 after executing the statement: num1=sqrt(7)^4
6. What is the value of num2 after executing the statement: num2=7^2
7. Write R code(s) to correctly compare num1 and num2 for equality
8. Write R codes(s) to generate a logic vector (a vector contains only TRUE or FALSE) that indicates if the corresponding value in vector numbers1 created in question 1 is greater than or equal to 5.
a. For example, the first value in numbers1 is 3, which is less than 5. Therefore its corresponding logic value is FALSE.
9. Given a variable val, write R code(s) to check if val is infinite, is not a number, and is not available.
10. Given a variable val2, write R code(s) to check if val2 is greater than 3 but less than or equal to 30.
11. Write R code(s) to check if any value in vector numbers1 created in question 1 is greater than 6.
Name your R script “PA1.R” and submit it by the due date indicated in the Course Calendar.
Explanation / Answer
# 1. Creating a vector called numbers1 using the colon operator :
numbers1 <- 3:7
# 2. Creating a vector called numbers2 using the c function
numbers2 <- c(3, 9, 27, 81, 105)
# 3. print element-wise products of numbers1 and numbers2
numbers1 * numbers2
# 4. print remainder for each of the item in numbers2 divided by 4
numbers2 %% 4
# 5. value of num1 after executing the statement: num1=sqrt(7)^4
num1=sqrt(7)^4
# 6. value of num2 after executing the statement: num2=7^2
num2=7^2
# 7. compare num1 and num2 for equality
num1 == num2
# 8. Creating a logic vector if the corresponding value in vector numbers1 is greater than or equal to 5
numbers1 >= 5
# 9. checking if val is infinite
is.infinite(val)
# if val is not a number
is.nan(val)
# if val is not available.
is.na(val)
# 10. checking if val2 is greater than 3 but less than or equal to 30.
val2 > 3 & val2 <= 30
# 11. logic vector if the corresponding value in vector numbers1 is greater than 6
numbers1 > 6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.