Description
Write a single R script (a .R file) that answers each of the following questions. Simply comment out text that you write to answer each question, and ensure that the code you write can be ran on any computer.
1. Create the vector
> myAtomicVector <- c(1, 4, 3, 2, NA, 3.22, -44, 2, NA, 0, 22, 34)
Now, create code that runs to answer each of the following questions.
(a) How many positive numbers (> 0) are there in this vector?
(b) How many negative numbers (< 0) are there in this vector?
(c) How many 0’s are there in this vector?
(d) How many NAs are there is this vector?
(e) How many numbers in the vector are non-zero and not NAs?
(f) What is the sum of the positive numbers is this vector?
(g) What is the sum of the negative numbers is this vector?
2. Create the following vectors, populated with information about the ICPSR summer courses in Session
I.
• coursename: course name of each course
• enrolled: a logical vector indicating whether or not you are enrolled in the course
• anticipatedGrade: your anticipated letter grade in each course, with an NA for any course for which you are not enrolled
• anticipatedHours: your anticipated hours spent on each class per week based on on your experience so far, with an NA for any course for which you are not enrolled.
3. Create and print a data frame called MyCourseDataFrame by combining all of the above vectors. Assign the names of each column to be the names of the original vectors. Summarize the type of each column. Do the data frame variables retain their original classes? Formally test this using appropriate R code.
4. Combine the vectors from (1) into a list called MyCourseDataList, where each vector is an element of the list. Assign the names of each element to be the names of the original vectors. Do the elements of the list maintain their original classes? Formally test this using appropriate R code.
1
5. Write code that returns the following values:
• The values in courseNum, excluding the fourth value
• The total number of hours you anticipate spending on coursework, both per week, and on average number of hours you anticipate spending for these courses.
• A data frame with only the third row and first two columns of myCourseDataFrame
• The first value in the second element of MyCourseDataList
6. Convert the anticipatedGrade variable in MyCourseDataFrame into a factor using the function factor(). Note: to get an ordering of values, you’ll have to use the argument is.ordered = TRUE. Look at the documentation of factor() to understand how to do this. Now write code to answer the following questions, and output the answers.
(a) What is the maximum letter grade you anticipate receiving this semester?
(b) What is the minimum number of hours you expect to work per week in a class this semester? Is this in this course?
2




Reviews
There are no reviews yet.