##############################################################################################.
##############################################################################################.
##
## TOPIC: DATAFRAMES
##
##############################################################################################.
##############################################################################################.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 46
# TOPICS: dataframes
#
# Write R code that creates a dataframe that has 2 columns.
#
# The first column should contain the even numbers
# from 2 through 1000, ie. 2,4,6 ... 1000.
#
# The 2nd column should contain the odd numbers
# from 999 counting down to 1, i.e. 999, 997, 995, ... 1
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41 39. Practice Questions - TOPIC: Dataframes
41.1 Dataframes
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 47
# TOPICS: dataframes
#
# Write R code that creates a dataframe that contains the following information:
#
# The name of the 1st column should be: "department"
# The values in this column should be: produce, bakery, produce, produce, bakery.
#
# The name of the 2nd column should be: "product"
# The values in this column should be: apple, chocolate cake, orange, pear, rye bread.
#
# The name of the 3rd column should be: "price"
# The values in this column should be: 1.99 , 1.59, 0.99, 1.99, 4.99.
#
# Store the dataframe in a variable named "prices".
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 48
# TOPICS: dataframes
#
# Write a command that changes the names of the columns in the dataframe
# from the previous question. The first column should now be named itemName and
# the 2nd column should be named pricePerLb. Hint: use the colnames function.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 49
# TOPICS: dataframes
#
# Challenge: Write a command that changes the names of the rows in the
# "prices" dataframe from the previous question. The new names of the rows
# should be item1, item2 , etc. Write the code so that no matter how many rows
# the dataframe would contain the names of the rows would follow the same pattern.
# For example, if the dataframe happened to contain 1000 rows, then after your
# command executed, they would be named: item1,item2,item3,item4,... item1000
#
# Hint: As part of your answer, you will need to use the paste function
# with sep="" (nothing between the quotes). Note that we did not cover the paste
# function in class. You should look at the help for the paste function and try a
# few examples of your own. You can also type the R command example(paste) to
# automatically run the examples that appear at the end of the help page for the
# paste function.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 50 (a)
# TOPICS: dataframes
#
# a. Write a command that modifies the dataframe from the previous question.
# The command should change the word "apple" to "mcintosh apple".
#
# b. Do this again, but this time DO NOT refer to the actual row number or
# column number in your answer. Your answer should work successfully,
# no matter what row contains "apple".
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 51
# TOPICS: dataframes
#
# Write a command that modifies the dataframe from the previous question.
# The command should increase the pricePerLb of oranges by 10%. Round the increased
# price to the nearest penny.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 52
# TOPICS: dataframes
#
# Write a command that modifies the "prices" dataframe from the previous question.
# The command should increase the price of all items by 10%. Round the increased prices
# to the nearest penny.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 53
# TOPICS: dataframes
#
# Refer to the dataframe from the previous question. Write a command that adds
# a new column named salePrice. The new column should contain the values from
# the price column reduced by 25%. Round the prices in the new column to the nearest penny.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 54
# TOPICS: dataframes
#
# Refer to the dataframe from the previous question.
# Write a command that displays the itemName and price of all items from the
# produce department. Write the code so that it will continue work even if the
# specific data in the dataframe changes.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 55
# TOPICS: dataframes
#
# Refer to the dataframe from the previous question.
# Write a command that displays the itemName and price of all items whose price
# is 2.00 or more. Write the code so that it will continue work even if the
# specific data in the dataframe changes.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 56
# TOPICS: dataframes
#
# Refer to the dataframe from the previous question.
# Write a command that displays all of the columns for all items whose
# price is 2.00 or more. Write the code so that it will continue work
# even if the specific data in the dataframe changes.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 57
# TOPICS: dataframes
#
# Refer to the dataframe from the previous question.
# Write a command that calculates the average pricePerLb of all
# items in the produce department.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 58
# TOPICS: dataframes
#
# Refer to the dataframe from the previous question.
# Write a command that displays all of the data from just the odd numbered rows.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 59
# TOPICS: dataframes
#
# Refer to the dataframe from the previous question.
# Write a command that displays just the name of the items whose
# pricePerLb is at least 1.00 but not more than 2.00.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 60
# TOPICS: dataframes
#
# Refer to the dataframe from the previous question.
# Write a command that displays just the name of the items whose
# pricePerLb is either less than 1.00 or more than 2.00.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 61
# TOPICS: dataframes
#
# Refer to the dataframe from the previous question.
# Write a command that displays just the name of the produce whose pricePerLb is at
# least 1.00 but not more than 2.00.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 62
# TOPICS: dataframes
#
# Refer to the dataframe from the previous question.
# Write a single command that displays
# the name of the produce whose pricePerLb is less than 1.00 and also
# the produce whose pricePerLb is greater than 2.00
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 63
# TOPICS: dataframes
#
# a. Write a command that displays all of the data for a "randomly chosen" row
# from the dataframe. Your answer should work even if the data in the dataframe changes.
#
# Hint: as part of your answer, use the nrow function to determine the number of rows
# that are in the dataframe.
#
# b. Write a command that displays the rows in the reverse order (ie last row first).
#
# c. Write a command that displays the rows in the dataframe in a randomly
# chosen order.
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 64
# TOPICS: dataframes
#
# Write a command that displays the average pricePerLb of those items
# whose pricePerLb is at least 1.50
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 65
# TOPICS: dataframes
#
# Write a command that displays the number of items that are listed in
# the "produce" department.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 66
# TOPICS: dataframes
#
# Write a command that displays the number of items that are listed
# in the "produce" department whose pricePerLb is at least 1.50.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~