Instructions

  1. Update the author line at the top to have your name in it.
  2. You must knit this document to an html file and publish it to RPubs. Once you have published your project to the web, you must submit the web url link into the appropriate Course Project assignment in Blackboard before 11:59pm on the due date.
  3. Answer all the following questions completely. Some may ask for written responses.
  4. Use R chunks for code to be evaluated where needed and always comment all of your code so the reader can understand what your code aims to accomplish.
  5. Proofread your knitted document before publishing it to ensure it looks the way you want it to. Tip: Use double spaces at the end of a line to create a line break and make sure normal text does not appear as a header.

Purpose

This first project will introduce you to creating RMarkdown files and doing basic data manipulation with R and RStudio.


Question 1

In a single r chunk, perform two operations: first add the numbers 9 and 23, then divide the number 42 by the sum of 83 and 101. State both answers in complete sentences beneath your r chunk, with your second answer rounded to three decimal places.

9 + 23
## [1] 32
42 / (83 + 101)
## [1] 0.2282609
The sum of nine (9) and twenty-three (23) is equal to thirty-two (32).
9 + 23 = 32

The quotient of forty-two (42) and the quantity of the sum of eighty-three (83) and one hundred one (101) is equal to zero point two two eight two six zero nine (0.2282609) within RStudio, although the exact floating point decimal is 0.22826086956521739130434782608695652173913043478260869565217391304347826086956521739130434782608695652173913043478260869565217391304347826086956521739130434782608695652173913043478260869565217391304348.

Question 2

In an r chunk below, store the dataframe mtcars into an object called mtcars and use the head() function to look at the first 6 rows of that object.

mtcars <- mtcars
head(mtcars, 2^2*1.5-14+16-2)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Use the ?mtcars command in the RStudio Console to pull up the help documentation to find out what the qsec variable describes in the mtcars dataset, then write your answer in a complete sentence. (No r chunk is required here).

The ‘qsec’ variable, as listed in the mtcars dataset and parsed by the Help function, lists the time in seconds it takes each vehicle to run the quarter mile standard.

Question 3

To load a package into R requires the use of the install.packages() and library() functions. Type: install.packages(“openintro”) into the RStudio Console to install that package on to your computer. The install.packages() function requires a character input, so ” ” are needed around the input.

The library() function takes in an object name, so do not use ” ” around the object name.

Use an r chunk below to load the library openintro which contains some datasets we will use going in subsequent problems.

# install.packages("openintro")
# library("openintro")

With the openintro library loaded, you can now access some datasets from it. In another r chunk, find the dimensions of the email dataset by using the dim() function on it.

# email <- email
# dim(email)

The dim() function returned a result of [1] 3921 and 21 when the above chunk was executed.

Question 4

The $ sign is used to specify a certain variable of a dataframe. For example, the following will give me the sum of the cylinders variable in the mtcars dataframe.

# Sum of the cyl variable in mtcars dataframe
sum(mtcars$cyl)
## [1] 198

In the email dataframe, what is the sum of the values in the line_breaks variable? State your answer in a complete sentence beneath the chunk.

# sum(email$line_breaks)

The sum of the values in the line_breaks variable within the dataframe email is nine hundred four thousand four hundred twelve (904412).

Question 5

In the email dataset, the first variable called spam has a value of 0 if the email was not spam and a value of 1 if the email was spam. You can use the table() function on the spam variable to see how many emails fell into the spam and non-spam categories.

Using an r chunk, calculate what percent of the emails in this dataframe were considered spam? Then state your answer beneath the chunk in a complete sentence, rounded to nearest tenth of a percent.

# First method which just calculates the entire vector and returns the results as a table below.
# round(table(email$spam)/sum(table(email$spam))*100,1)

# Second method, which I had to correct the dataset which stored the email$spam variable as a factor rather than an integer by converting it to character and then numeric.
# round(sum(as.numeric(as.character(email$spam)))/sum(table(email$spam))*100,1)

In the dataset emails, the percentage of the emails which are marked as spam, rounded to the nearest tenth of a percent, is nine point four percent (9.4%).

Question 6

We can use the subset() function to get subsets of a dataframe based on a given characteristic. For example, the code below will store a new dataframe that only contains the observations in which the word “dollar” or a $ sign was found in the email.

# Storing emails with "dollar" in them to an object called money
# money <- subset(email, email$dollar > 0)

Use an r chunk to find the percent of emails in the money object that are spam.

# table(email$spam)
# table(money$spam)
78/367*100
## [1] 21.25341

The percent of emails in the money object that are spam, rounded to the nearest tenth of a percent, is 21.3%.

Comparing your answers from #7 and #8, do dollar signs or the word “dollar” seem to be a key characteristic of emails that are spam? Explain why or why not.

Assuming you meant questions #5 and #6, given that those are the previous two questions, the answer to this prompt would be a resounding ‘no’. At just under one quarter of all spam emails, the characteristic of including a dollar sign represents nearly one quartile of the category, but it does not appear to be a key characteristic.

Question 7

Type View(airquality) into your RStudio console (not in an r chunk) and you will see a tab open up with the dataset in it for viewing. The dataframe gives air quality measurements in NYC over a 153 day period in the year 1973. In the chunk below, I have stored the airquality dataframe into an object called airquality and returned the names of the variables.

# Store airquality dataframe in object called airquality
airquality <- airquality

# Find the names of the variables in the dataframe
names(airquality)
## [1] "Ozone"   "Solar.R" "Wind"    "Temp"    "Month"   "Day"

Type ?airquality into the RStudio console to look at the documentation for the dataset and find out what the variable names in the dataframe represent.
This chunk below tries to find the sum of the solar radiation in langleys (a unit of solar radiation), but there is a problem, it returns NA.

# Find sum of Solar.R variable
sum(airquality$Solar.R)
## [1] NA

If you type View(airquality) into your RStudio console again, you can see that there are some values in the Solar.R variable, but also some NA values, which the sum function cannot handle.

Type ?sum into your RStudio console and look at the help documentation for that function. You’ll see in the Usage section sum(..., na.rm = FALSE). The second argument, na.rm, stands for “remove NAs” and this function defaults to FALSE, or no, do not remove NAs. If we want the sum function to ignore the NAs in a vector, we just need to set na.rm = TRUE as a second argument inside the function.

Use an r chunk to find the sum of the solar radiation (the Solar.R variable) by ignoring NAs.

sum(airquality$Solar.R, na.rm = TRUE)
## [1] 27146

The sum of the solar radiation from airquality in NYC is equal to 27,146 Langleys.

Question 8

Often we will need to load our own data into R for analysis. A common file type that data is stored in is called a “.csv” file or “comma separated values”. The common way to load a .csv in R is with the read.csv() function and storing it to an object.

Example:
new_data <- read.csv("newdata.csv")

For this question:

  • Download the mens_health csv file from the “Additional Datasets” folder in Blackboard and save it your computer
  • Load the mens_health file into your R Environment. One way to do this is to use the Import Dataset button to load the file. You will then need to copy the line of code from the RStudio Console into an r chunk below in this RMarkdown document to ensure it runs in your final knitted project.
  • Look at the structure of the dataframe by using the str() function
  • In an r chunk below, find the sum of any numeric variable in that dataset.
mens_health <- read.csv("mens_health.csv")
str(mens_health)
## 'data.frame':    40 obs. of  14 variables:
##  $ MALE : int  1391 2129 2489 2490 2738 2988 2989 3346 3606 3607 ...
##  $ AGE  : int  58 22 32 31 28 46 41 56 20 54 ...
##  $ HT   : num  70.8 66.2 71.7 68.7 67.6 69.2 66.5 67.2 68.3 65.6 ...
##  $ WT   : num  169 144 179 176 153 ...
##  $ WAIST: num  90.6 78.1 96.5 87.7 87.1 ...
##  $ PULSE: int  68 64 88 72 64 72 60 88 76 60 ...
##  $ SYS  : int  125 107 126 110 110 107 113 126 137 110 ...
##  $ DIAS : int  78 54 81 68 66 83 71 72 85 71 ...
##  $ CHOL : int  522 127 740 49 230 316 590 466 121 578 ...
##  $ BMI  : num  23.8 23.2 24.6 26.2 23.5 24.5 21.5 31.4 26.4 22.7 ...
##  $ LEG  : num  42.5 40.2 44.4 42.8 40 47.3 43.4 40.1 42.1 36 ...
##  $ ELBOW: num  7.7 7.6 7.3 7.5 7.1 7.1 6.5 7.5 7.5 6.9 ...
##  $ WRIST: num  6.4 6.2 5.8 5.9 6 5.8 5.2 5.6 5.5 5.5 ...
##  $ ARM  : num  31.9 31 32.7 33.4 30.1 30.5 27.6 38 32 29.3 ...
sum(mens_health$AGE)
## [1] 1419

The sum of AGE in mens_health.csv is 1419.