#######################################################################
# Given the following parallel vectors answer the questions below
#######################################################################
# The following are parallel vectors
<- c("joe", "sue", "al", "anne", "esther", "bob", "larry", "carla")
students <- c("m", "f", "m", "f", "f", "m", "m", "f")
gender <- c("senior", "freshman", "senior", "sophomore", "senior", "senior", "freshman", "sophomore")
year <- c(85, 82, 70, 95, 93, 100, 39, 90)
test1 <- c(95, 60, 75, 92, 93, 90, 68, 80)
test2 <- c(100, 69, 79, 81, 93, 80, 89, 82)
test3 <- c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE)
honors <- c("ca", "nj", "wa", "ny", "ny", "ca", "nj", "wa")
homeState<- c("fin", NA, "acc", NA, "mar", "acc", "acc", "fin")
major
#-----------------------------------------------------------------------------------
# Answer the questions below by writing R commands to calculate the answers.
# The answers should still be correct even if the actual values in the vectors change.
# You can assume that there are no NA values in the data EXCEPT for in the vector, major
# If a student hasn't declared a major yet, the entry in the major vector is NA.
#-----------------------------------------------------------------------------------
41 Practice Questions - TOPICS: Parallel Vectors, &, |, scientific notation, etc.
41.1 QUESTION 1
#-----------------------------------------------------------------------------------
# QUESTION 1
# Show the number of grades on test1 that were below 70
#-----------------------------------------------------------------------------------
41.2 QUESTION 2
#-----------------------------------------------------------------------------------
# QUESTION 2
# Show the number of grades below 70 on any of the tests
#-----------------------------------------------------------------------------------
41.3 QUESTION 3
#-----------------------------------------------------------------------------------
# QUESTION 3
# Show the percent of seniors who are from NY
#-----------------------------------------------------------------------------------
41.4 QUESTION 4
#-----------------------------------------------------------------------------------
# QUESTION 4
# Show the number of students who have not yet declared a major (i.e. major is NA)
#-----------------------------------------------------------------------------------
41.5 QUESTION 5
#-----------------------------------------------------------------------------------
# QUESTION 5
# Show the number of students
#-----------------------------------------------------------------------------------
41.6 QUESTION 6
#-----------------------------------------------------------------------------------
# QUESTION 6
# Show the names of the students who scored at least 5 points above average on test1
# test1
#-----------------------------------------------------------------------------------
41.7 QUESTION 7
#-----------------------------------------------------------------------------------
# QUESTION 7
# Show the names of the students who scored at least 5 points above average on test1
# and also on test2
#-----------------------------------------------------------------------------------
41.8 QUESTION 8
#-----------------------------------------------------------------------------------
# QUESTION 8
# Show the names of the students who scored at least 5 points above average
# on either test1 or test2
#-----------------------------------------------------------------------------------
41.9 QUESTION 9
#-----------------------------------------------------------------------------------
# QUESTION 9
# Show the names of the male students
# who scored at least 5 points above average on either test1 or on test2
#-----------------------------------------------------------------------------------
41.10 QUESTION 10
#-----------------------------------------------------------------------------------
# QUESTION 10
# Show the number of accounting (acc) majors.
#
# NOTE: Be careful - some of the entries in the majors vector are NA.
#-----------------------------------------------------------------------------------
41.11 QUESTION 11
#-----------------------------------------------------------------------------------
# QUESTION 11
# Show the number of students who have NOT declared that
# they are acc majors (this includes students who have not yet declared a major).
#
# NOTE: Be careful - some of the entries in the majors vector are NA.
#-----------------------------------------------------------------------------------
41.12 QUESTION 12
#-----------------------------------------------------------------------------------
# QUESTION 12
# Show the percent of the students who have declared that they are acc majors.
#
# NOTE: Be careful - some of the entries in the majors vector are NA.
#-----------------------------------------------------------------------------------
41.13 QUESTION 13
#-----------------------------------------------------------------------------------
# QUESTION 13
# Show the percent of the seniors who have declared that they are acc majors.
#
#
# NOTE: Be careful - some of the entries in the majors vector are NA.
#-----------------------------------------------------------------------------------
41.14 QUESTION 14
#-----------------------------------------------------------------------------------
# QUESTION 14
# Write code to show the names of the students who continually got lower grades.
# ie test1 was the highest and test3 was the lowest grade
#-----------------------------------------------------------------------------------
41.15 QUESTION 15
#-----------------------------------------------------------------------------------
# QUESTION 15
# Show the students who got 100 on all three tests
#-----------------------------------------------------------------------------------
41.16 QUESTION 16
#-----------------------------------------------------------------------------------
# QUESTION 16
# Show the students who got the same grade for all three tests
#-----------------------------------------------------------------------------------
41.17 QUESTION 17
#-----------------------------------------------------------------------------------
# QUESTION 17
# Show the freshemn and sophomores whose highest grade was on the third test
#-----------------------------------------------------------------------------------
41.18 QUESTION 18
#-----------------------------------------------------------------------------------
# QUESTION 18
# Show the average grade on test1 of honors students who are also seniors
#-----------------------------------------------------------------------------------
41.19 QUESTION 19
#-----------------------------------------------------------------------------------
# QUESTION 19
# Use a single command to create a vector that contains
# the average grade on test1 the honors students in the first position and
# the average grade on test1 for the non honors students in the 2nd position
#-----------------------------------------------------------------------------------
41.20 QUESTION 20
#-----------------------------------------------------------------------------------
# QUESTION 20
# Write a command that shows the names of the honors students who are from NY, NJ or CT
#-----------------------------------------------------------------------------------
41.21 QUESTION 21
#-----------------------------------------------------------------------------------
# QUESTION 21
# Answer the previous question without using the | operator.
#-----------------------------------------------------------------------------------
41.22 QUESTION 22
#-----------------------------------------------------------------------------------
# QUESTION 22
#
# Create a vector that shows the students name, gender and homeState
# for all of the female students in the following format:
#
# "sue;f;nj" "anne;f;ny" "esther;f;ny" "carla;f;wa"
#-----------------------------------------------------------------------------------
41.23 QUESTION 23
#-----------------------------------------------------------------------------------
# QUESTION 23
#
# Create a single value that has one "m" for each male student and one "f" for
# each female student. The f's should be first before the m's. The f's and m's should
# be separated from each other with dashes (see example below).
# For example, if there would be 5 female and 2 male students your code should show:
#
# "f-f-f-f-f-m-m"
#
# Remember that your code should work even if the current data changes. (I don't know
# what the data that you get will be.)
#-----------------------------------------------------------------------------------
41.24 QUESTION 24
#-----------------------------------------------------------------------------------
# QUESTION 24
# Write a command that displays the names and majors of the studets who are NOT honors students.
# The output should be in the following format: each student should be on a separate line.
# There should be a tab between the student's name and their major. See example output below.
# (NOTE that the results should be correct even if the actual data changes):
#
# sue NA
# al acc
# esther mar
# bob acc
# larry acc
#-----------------------------------------------------------------------------------
41.25 QUESTION 25
#-----------------------------------------------------------------------------------
# QUESTION 25
# Write a function named isEven that takes a single argument named nums that is
# expected to be a numeric vector.
# The function should return a logical vector.
# Each entry in the logical vector should be TRUE if the corresponding number is even
# and FALSE if the corresponding number is odd.
#
# EXAMPLE:
# # DEFINE THE FUNCTION isEven HERE
# > isEven ( c(2, 13, 23, 14) )
# [1] TRUE FALSE FALSE TRUE
#-----------------------------------------------------------------------------------
41.26 QUESTION 26
#-----------------------------------------------------------------------------------
# QUESTION 26
# Write a command that uses the function isEven that you created in the
# previous question to display those test1 grades that are even.
#-----------------------------------------------------------------------------------
41.27 QUESTION 27
#-----------------------------------------------------------------------------------
# QUESTION 27
# Use the isEven function that you created above to create a new function named isOdd.
# isOdd should work similarly to isEvn but isOdd should return a vector that contains
# TRUE if a number is odd and FALSE if the number is even.
#
# EXAMPLE:
# # DEFINE THE FUNCTION isEven HERE
# > isOdd ( c(2, 13, 23, 14) )
# [1] FALSE TRUE TRUE FALSE
#-----------------------------------------------------------------------------------
41.28 QUESTION 28
#-----------------------------------------------------------------------------------
# QUESTION 28
# Use the isOdd function that you described above to display the names
# of the students who got odd numbered grades on test1.
# Show their grades below their names.
#-----------------------------------------------------------------------------------
41.29 QUESTION 29
#-----------------------------------------------------------------------------------
# QUESTION 29
# Write a single command to display the following. The 2nd and 3rd lines
# should each be indented by one tab. Make sure to get all the 'apostrophes'
# and "quotes" displayed correctly.
#
# Joe's parrot said:
# "I want a cracker.
# I want it now."
#-----------------------------------------------------------------------------------
41.30 QUESTION 30
#-----------------------------------------------------------------------------------
# QUESTION 30
#
# The following "numbers" were entered using "quotes".
# What will be the output from the following line of code?
#
# > nums = c("2000", "9", "1", "350", "3" )
# > sort(nums)
#
# Fill in the blank in the following code so that the numbers
# sort in numeric order. Do not change anything else in the code.
# You answer should work no matter what "numbers" are actually in
# the code.
#
# > nums = c("2000", "9", "1", "350", "3" )
# > FILL IN YOUR ANSWER HERE SO THAT THE NEXT LINE PRODUCES THE OUTPUT SHOWN BELOW
# > sort(nums)
# [1] 1 3 9 350 2000
#-----------------------------------------------------------------------------------
41.31 QUESTION 31
#-----------------------------------------------------------------------------------
# QUESTION 31
#
# The grades vector shown below shows the grades for a test.
# The grades for some students who missed the test were entered as NA.
# Fill in a command to get the average of the grades - not counting the NA grades.
#
# > grades = c(NA, 75, NA, 85, 90, 70 )
# > # COMMAND TO GET AVERAGE - IGNORE THE "missing" or NA grades
# [1]
#-----------------------------------------------------------------------------------
41.32 QUESTION 32
#-----------------------------------------------------------------------------------
# QUESTION 32
#
# The grades vector shown below shows the grades for a test.
# The numbers were incorrectly entered using "quotes".
# The grades for some students who missed the test were entered as NA
# and for others the grades were entered as "missing".
# Fill in code as shown below so that you generate average.
# Note that a warning may be shown - that is fine.
#
# > grades = c(NA, "75", NA, "85", "90", "missing", "70" )
# > # COMMAND TO GET AVERAGE - IGNORE THE "missing" or NA grades
# [1] 80
# Warning message: ..... (some warning messages may be shown)
#-----------------------------------------------------------------------------------
41.33 QUESTION 33
#-----------------------------------------------------------------------------------
# QUESTION 33
#
# It is VERY important to understand how to use R's debugger.
# This question tests your knowledge of how to use R's debugger.
# You do NOT have to write code to answer this question.
# Rather your job is to write what you see as the debugger runs
# as described in the instructions below.
#
# Refer to the function, strangeFunction, that appears below these instructions.
# This function is written with several advanced R concepts that you did not learn
# about yet. You are not expected to understand all of this code. Keep reading
# to understand what you ARE supposed to do.
#
#
# strangeFunction = function(){
# yourName = "John Doe"
# functionText = paste0(capture.output(strangeFunction),collapse="")
# functionText = strsplit(functionText,"")[[1]]
# functionText = functionText[ ! ( functionText %in% c("\n","\t"," ","\r")) ]
# endOfFunction = max(which (functionText == "}"))
# functionText = functionText[ 1:endOfFunction ]
# s = sum(sapply(functionText,function(c)as.numeric(charToRaw(c))))
# set.seed(s)
# x = trunc(runif(1) * 10000)
# message = paste0("Don't pay attention to this return value.",
# " What is the value of x as the function runs?",
# " Use the debugger to figure it out.",
# " By the way, if you change the code in the function AT ALL",
# " the answer will be different :). The only way to",
# " get the right answer is to use the debugger.")
# return(message)
# }
#
# strangeFunction, doesn't seem to do anything useful
# strangeFunction() will always output the following message:
#
# > strangeFunction()
# [1] "Don't pay attention to this return value.
# What is the value of x as the function runs?
# Use the debugger to figure it out.
# By the way, if you change the code in the
# function AT ALL the answer will be different :).
# The only way to get the right answer is to use the debugger."
#
# To answer this question do the following:
#
# 1. Change the value of the variable yourName to your actual name.
# The function currently has:
# yourName = "John Doe"
#
# Change "John Doe" to your actual name, for example:
# yourName = "Michael Greenspilzingower"
#
#
# 2. The following line appears in the function:
#
# x = trunc(runif(1,1,100000))
#
# Use the debugger to find the value of x as the function runs.
#
# Note that everyone should have their own name in the variable yourName.
# This function uses some advanced R concepts to ensure that
# changing the code of the function will change the value that is
# assigned to x in unpredictable ways. Therefore since everyone
# changed the code of the function by adding their own name, everyone should
# have a different answer to this question.
#
# Your answer should include a copy of your version of the function as well
# as the value of x that you got from the debugger.
# For example, Michael Greenspilzingower should report his answer as follows:
#
# For my version of the function, the value of x is : 9281
# My version of the function is below.
#
# strangeFunction = function(){
# yourName = "Michael Greenspilzingower"
# functionText = paste0(capture.output(strangeFunction),collapse="")
# functionText = strsplit(functionText,"")[[1]]
# functionText = functionText[ ! ( functionText %in% c("\n","\t"," ","\r")) ]
# endOfFunction = max(which (functionText == "}"))
# functionText = functionText[ 1:endOfFunction ]
# s = sum(sapply(functionText,function(c)as.numeric(charToRaw(c))))
# set.seed(s)
# x = trunc(runif(1) * 10000)
# message = paste0("Don't pay attention to this return value.",
# " What is the value of x as the function runs?",
# " Use the debugger to figure it out.",
# " By the way, if you change the code in the function AT ALL",
# " the answer will be different :). The only way to",
# " get the right answer is to use the debugger.")
# return(message)
# }
#
# NOTE: If you leave the name as John Doe, then the value of x is 7388.
# You can use this to check to make sure that you are doing the question
# correctly before trying your own name.
#-----------------------------------------------------------------------------------
41.34 QUESTION 34
#-----------------------------------------------------------------------------------
# QUESTION 34
#
# create a function named equal values that takes two vectors, v1 and v2,
# and returns a vector of those values that are equal and in the same
# positions in the two vectors.
#
# EXAMPLE
# > equal( c(100,200,300,400), c(50, 200, 60, 400))
# [1] 200 400
#-----------------------------------------------------------------------------------
41.35 QUESTION 35
#-----------------------------------------------------------------------------------
# QUESTION 35
#
# Create a function named mixAndPaste that takes the following arguments and
# returns the value described below.
#
# Arguments
# x - a vector
# y - a vector of the same length as x
#
# Return value
# A character vector that pastes the values of x and y together.
# The 1st, 3rd, 5th, etc positions of the return value should have the
# x value before the y value.
# The 2nd, 4th, 6th, etc positions of the return value should have the
# y value before the x value.
#
# EXAMPLE1
# > mixAndPaste( c("a","b","c","d","e","f"), c("u","v","w","x","y","z"))
# [1] "au" "vb" "cw" "xd" "ey" "zf"
#
# EXAMPLE2
# > mixAndPaste(c("apple", "pear", "lemon", "plum"),c("RED", "GREEN", "YELLOW", "PURPLE"))
# [1] "appleRED" "GREENpear" "lemonYELLOW" "PURPLEplum"
#
# HINTS
# a. You can use more than one line of code in the function definition.
# b. You can create new "local" variables in the function definition.
# c. Figure out how to create two new local variables in the function.
# One of the local variables should contain the first vector to be pasted
# (e.g. using the values from EXAMPLE1 above, this first vector
# would contain "a" "v" "c" "x" "e" "z")
# and another vector should contain the second vector to be pasted
# (e.g. using the values from EXAMPLE1 above, this second vector
# would contain "u" "b" "w" "d" y" "f".
# Then the function should reutrn the values of those vectors pasted together.
# d. To accomplish c. above, remember that you can assign values to specific
# locations in a vector, as long as that vector already exists.
#-----------------------------------------------------------------------------------
41.36 QUESTION 36
#-----------------------------------------------------------------------------------
# QUESTION 36
#
# The built-in function, which, returns the positions in a logical vector that are TRUE.
# For example
#
# > which ( c(FALSE, TRUE, FALSE, FALSE, TRUE)) # 2 5 because those positions are TRUE
# [1] 2 5
#
# > nums = c(9999,3,950,222,-5)
# > which(nums > 100) # 1 3 4 because the 1st, 3rd and 4th values are greater than 100
# [1] 1 3 4
#
# > which ( c(FALSE, FALSE) ) # integer(0) ... this means that no values are TRUE
# integer(0)
#
# Create a function named, myWhich, that does the same thing as the built-in, "which" function.
# Your function should takee a single argument named x.
# Do NOT use the which command in your code.
#
# EXAMPLE1
# > myWhich ( c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE)) # 1 2 5 because those positions are TRUE
# [1] 1 2 5
#
# > nums = c(10,3,950,222,-5)
# > myWhich(nums > 100) # 3 4 because the 3rd and 4th position values are greater than 100
# [1] 3 4
#
# > myWhich ( c(FALSE, FALSE) ) # integer(0) ... this means that no values are TRUE
# integer(0)
#
# HINTS
# - Create a local variable that contains the position numbers from 1 until the last
# position in x. Return some of the values from that local variable.
#-----------------------------------------------------------------------------------
41.37 QUESTION 37
#-----------------------------------------------------------------------------------
# QUESTION 37
#
# Create a function named nextValue that takes the following arguments:
#
# Arguments:
# vec - expected to be a vector
# position - is a position in the vector, expected to be between 1 and length(wordVec)
#
# There is obviously a value at the specified position in vec.
# The function should return the value from vec that is alphabetically (if vec is character)
# or numerically (if vec is numeric) right after the specified value. See the examples below.
#
# HINTS
# - you may create local variables in your function to help you
# work through the ideas
#
# - use the sort function to help you
#
# - realize that positions after the last value in a vector are NA
#
#
#
# EXAMPLE1
# > # banana is in the 4th position, "car" is alphabetically after "banana"
# > nextValue (vec = c("fox", "car", "apple", "banana", "deer", "ball", "elephant"),
# position = 4)
# [1] "car"
#
# EXAMPLE2 (same words as example1)
# > # "deer" is in the 5th position, "elephant" is alphabetically after "deer"
# > nextValue (vec = c("fox", "car", "apple", "banana", "deer", "ball", "elephant"),
# position = 5)
# [1] "elephant"
#
# EXAMPLE3 (same words as example1)
# > # The 1st word is "fox" and it is the last word alphabetically, so return NA
# > nextValue (vec = c("fox", "car", "apple", "banana", "deer", "ball", "elephant"),
# position = 1)
# [1] NA
#
# EXAMPLE4 (this time with a numeric vector)
# > # In this example, the value in the 2nd position is 425.
# > # 690 is the next value numerically after 425.
# > nextValue ( c(100, 425, 50, 400, 690, 800, 200), 2 ) # 690
# [1] 690
#-----------------------------------------------------------------------------------
41.38 QUESTION 38
#-----------------------------------------------------------------------------------
# QUESTION 38
#
# Create a function, wordsBetween
# Arguments:
# wordVec - expected to be a character vector of words (or any other character data)
# wordA - a single word
# wordB - another single word
#
# The function should return a vector of the words from wordVec that
# appear in wordsBetween that are alphabetically between wordA and wordB.
#
# EXAMPLE
# # DEFINE FUNCTION wordsBetween HERE
# > words = c("car", "bicycle", "harley", "truck", "boat", "caravan", "minivan", "plane")
# > wordsBetween(words, "cat", "monkey")
# [1] "harley" "minivan"
#-----------------------------------------------------------------------------------
41.39 QUESTION 39
#-----------------------------------------------------------------------------------
# QUESTION 39
#
# Write the number 0.00031 in R's version of scientific notation.
#-----------------------------------------------------------------------------------
41.40 QUESTION 40
#-----------------------------------------------------------------------------------
# QUESTION 40
#
# After running an R command R responded with
# [1] 1.23e+12
# Explain what that means. Write that number in a more familiar form?
#-----------------------------------------------------------------------------------