#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 73
# TOPIC: vectors
#
# Show the last 5% of the values in a vector named v.
# For example
# - if there are 100 values in v, show the last 5 values
# - if there are 200 values in v, show the last 10 values
# - etc.
# - if there are 43 values in v, 5% of 43 is 2.15 so round up (use ceiling function)
# to show 3 last values
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40 38. Practice Questions - TOPIC: Assorted Topics
40.1 Additional Questions - Assorted Topics
QUESTION 73 TOPIC: vectors
QUESTION 74 TOPIC: dataframes
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 74
# TOPIC: dataframes
#
# Show the last 5% of the rows of the data in the dataframe named df.
# For example
# - if there are 100 rows, show the last 5 rows
# - if there are 200 rows, show the last 10 rows
# - etc.
# - if there are 43 rows, 5% of 43 is 2.15 so round up (use ceiling function)
# to show 3 last rows
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
QUESTION 75 TOPIC: user defined functions
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 75
# TOPIC: user defined functions (creating your own functions)
#
# Write a function with the "signature", evensBetween = function(x,y).
# x and y are expected to be numbers (they may be even or odd).
# The function should return the even numbers between x and y (including x and y)
# You may assume that the value of x is less than the value of y.
#
# For example:
# > evensBetween(3,9)
# [1] 4 6 8
#
# > evensBetween(2,10)
# [1] 2 4 6 8 10
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
QUESTION 76 TOPIC: user defined functions
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 76
# TOPIC: user defined functions
# SEE ALSO: question 11 for a similar question that doesn't require the creation of a function
#
# Create a function with the signature, swapFirstAndLast = function(vec,num).
# The vector vec is expected to contain at least 2*num values.
# The function should return a vector that contains the same values as vec except
# the first "num" values in vec should be last and the last "num" values in vec should be first
# (see the examples).
#
# Examples:
# > swapFirstAndLast( seq(10,100,by=10), 3)
# [1] 80 90 100 40 50 60 70 10 20 30
#
# > swapFirstAndLast( seq(10,100,by=10), 2)
# [1] 90 100 30 40 50 60 70 80 10 20
#
# > swapFirstAndLast( c("abe","bob","carla","dana","ed","frank"), 1)
# [1] "frank" "bob" "carla" "dana" "ed" "abe"
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
QUESTION 77 TOPIC: user defined functions (creating your own functions)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 77
# TOPIC: user defined functions (creating your own functions)
# (see question 11 for a similar question that doesn't require the creation of a function)
#
# Define a function with the signature: collapsevector = function( vec )
# vec is expected to be a vector with an even number of entries.
#
# The function should return a vector that contains
# the sum of the first two items from vec
# followed by the sum of the next two items from vec
# followed by the sum of the next two items from vec
# etc.
#
# Examples:
# > collapseVector( c(10,20,30,40,50,60,70,80) )
# [1] 30 70 110 150
#
# > collapseVector( c(3,4,10,2,100,3))
# [1] 7 12 103
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
QUESTION 78 TOPICS: user defined functions (creating your own functions)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 78
# TOPICS: user defined functions (creating your own functions)
#
# Define a function with the signature: get2ndLowest = function( vec )
# that returns the 2nd lowest value in vec.
#
# EXAMPLES:
# > get2ndLowest(c(423,6,234,3,7,1))
# [1] 3
#
# > get2ndLowest(c(3,9,10,-23,-59,200))
# [1] -23
#
# > get2ndLowest( c("grapes", "apple", "plum", "banana", "pear") )
# [1] "banana"
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
QUESTION 79 TOPICS: vectors
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 79
# TOPICS: vectors
#
# x and y are two vectors of the same length
# combine x and y into a new vector named answer that contains
# the odd position values from x and the even position values of y.
#
# EXAMPLES
# > x = c(10,20,30,40)
# > y = c(100, 200, 300, 400)
# > # YOUR CODE GOES HERE (it can be a few lines if you like)
# > answer
# [1] 10 200 30 400
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
QUESTION 80 TOPICS: lists, loops, user defined functions
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 80
# TOPICS: lists, loops, user defined functions
# SEE ALSO: question 72 for a similar question that doesn't use loops
#
# Write a function with the following "signature":
#
# SIGNATURE: getAllPairs = function (vec)
#
# ARGUMENTS/PARAMETERS
#
# vec - a vector that contains 2 or more different values
# (you may assume that all values are different from each other)
#
# RETURNS:
# A list that contains a separate vector for each pair of values
# from vec. The list should include separate entries for each order of
# the pair of values (see below). Do not insert entries in the
# list for which both values are the same.
#
# EXAMPLE 1:
#
# > getAllPairs(c("apple", "orange"))
# [[1]]
# [1] "apple" "orange"
#
# [[2]]
# [1] "orange" "apple"
#
# EXAMPLE 2:
# > getAllPairs(c("apple", "orange", "pear"))
# [[1]]
# [1] "apple" "orange"
#
# [[2]]
# [1] "apple" "pear"
#
# [[3]]
# [1] "orange" "apple"
#
# [[4]]
# [1] "orange" "pear"
#
# [[5]]
# [1] "pear" "apple"
#
# [[6]]
# [1] "pear" "orange"#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
QUESTION 81 TOPICS: user defined functions, vectors
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 81 TOPICS: user defined functions, vectors
#
# Write a function with the following "signature":
#
# SIGNATURE: getTopTwoValues = function (vec)
#
# ARGUMENTS/PARAMETERS
#
# vec - a vector that contains 2 or more values
#
# Returns a vector that contains the two largest
# values from the vector,vec . The values should
# be returned IN INCREASING ORDER regardless
# of the order in the original vector. For example:
# > getTopTwoValues(c(1,4,2,5,3))
# 4 5
# > getTopTwoValues(c(1,5,2,4,3))
# 4 5
# If the largest value in vec appears more than once
# then the return value should include that value twice.
# For example:
# > getTopTwoValues(c(1,4,2,4,3,4))
# 4 4
#
# Hints: You can write the function in many different ways.
# Some functions that might help are: sort, head, tail
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~