act4_tonyzhang
pdf
keyboard_arrow_up
School
Pennsylvania State University *
*We aren’t endorsed by this school
Course
184
Subject
Mathematics
Date
Apr 3, 2024
Type
Pages
5
Uploaded by MajorMaskMule13
Activity 4
Tony Zhang
2/13/2024
Use Headers
Use headers to organize your document. The first level heading is denoted by a single pound sign/hash tag,
#
. Each new
problem/exercise should get a Level 1 Heading. For subparts, increase the heading level by increasing the number of hash
tags.
For example, if Problem 1 has Parts A (with parts i-ii) and B, your R Markdown file would have the following:
# Problem 1
[text]
## Part A
[text]
### Part i
[text]
### Part ii
[text]
## Part B
[text]
Code
There are two ways to include code in your document: inline and chunks.
Inline Code
To add inline code, you’ll need to type a grave mark ‘ (the key to the left of the numeral 1 key), followed by a lower case r,
a space, then the
R
commands you wish to r and a final grave. For example ‘
r nrow(dataFrame)
‘ would return the number
of rows in the data frame named “dataFrame”.
Inline code is good for calling values you have stored and doing quick calculations on those values. Inline code will not be
added to the Code Appendix.
Code Chunks
For more complicated code such as data manipulation and cleaning, creating graphs or tables, model building and testing,
you’ll want to use code chunks. You can do this in two ways:
•
You can click the Insert button found just above the RStudio’s editor page (has an icon of a white circle with a green
plus sign and a green square with a white C) and selecting R from the drop down list.
•
You can create your own code chunk by typing three graves in a row, returning twice and typing three more graves.
You should see the editor become shaded gray for those three lines. You will want to write your code starting in the
middle blank line. In the first line, right after the third grave, you’ll want to set options including coding language and
chunk name as well as other options (e.g., figure caption and dimensions).
1
Mathematics
To type mathematical formulas, you will need to use LaTeX commands. For inline mathematics you’ll need to enclose your
mathematical expression in \( and \). For display math (on it’s own line and centered), enclose the expression in \[ and \].
The following code will automatically create your Code Appendix by grabbing all of your code chunks and writing that code
here. Take a moment to look through the appendix and make sure that your code is fully readable. Use comments in your
code to help create markers for what code does what.
2
Code Appendix
# This template file is based off of a template created by Alex Hayes
# https://github.com/alexpghayes/rmarkdown_homework_template
# Setting Document Options
knitr
::
opts_chunk
$
set
(
echo =
TRUE
,
warning =
FALSE
,
message =
FALSE
,
fig.align =
"center"
)
install.packages
(
"ggplot2"
,
repos =
"http://cran.us.r-project.org"
)
install.packages
(
"janitor"
,
repos =
"http://cran.us.r-project.org"
)
library
(ggplot2)
library
(janitor)
data
(
"diamonds"
)
dim
(diamonds)
str
(diamonds)
diamonds2
<-
diamonds
diamonds2
<-
diamonds2[,
!
(
names
(diamonds2)
%in%
c
(
"x"
,
"y"
,
"z"
))]
head
(diamonds2,
3
)
frequency
<-
tabyl
(diamonds
$
clarity)
diamonds2
$
clarity4group
<-
diamonds2
$
clarity
diamonds2
$
clarity4group
<-
ifelse
(diamonds2
$
clarity
%in%
c
(
"I1"
),
"I1"
,
ifelse
(diamonds2
$
clarity
%in%
c
(
"SI2"
,
"SI1"
),
"S"
,
ifelse
(diamonds2
$
clarity
%in%
c
(
"VS2"
,
"VS1"
,
"VVS2"
,
"VVS1"
),
"VS"
,
"
diamonds2
$
clarity4group
<-
factor
(diamonds2
$
clarity4group,
levels =
c
(
"I1"
,
"S"
,
"VS"
,
"IF"
),
ordered =
TRUE
)
diamonds2
$
unit_price
<-
diamonds2
$
price
/
diamonds2
$
carat
str
(diamonds2)
clarity4group_frequency
<-
tabyl
(diamonds2
$
clarity4group)
clarity4group_frequency
averages
<-
aggregate
(
cbind
(carat, depth, price, unit_price)
~
cut,
data =
diamonds2,
FUN =
mean)
averages
Problem 1
Part A
install.packages
(
"ggplot2"
,
repos =
"http://cran.us.r-project.org"
)
## package ’ggplot2’ successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
##
C:\Users\tonyz\AppData\Local\Temp\RtmpofQ3po\downloaded_packages
install.packages
(
"janitor"
,
repos =
"http://cran.us.r-project.org"
)
## package ’janitor’ successfully unpacked and MD5 sums checked
3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
##
## The downloaded binary packages are in
##
C:\Users\tonyz\AppData\Local\Temp\RtmpofQ3po\downloaded_packages
library
(ggplot2)
library
(janitor)
data
(
"diamonds"
)
dim
(diamonds)
## [1] 53940
10
str
(diamonds)
## tibble [53,940 x 10] (S3: tbl_df/tbl/data.frame)
##
$ carat
: num [1:53940] 0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
##
$ cut
: Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
##
$ color
: Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ...
##
$ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ...
##
$ depth
: num [1:53940] 61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
##
$ table
: num [1:53940] 55 61 65 58 58 57 57 55 61 61 ...
##
$ price
: int [1:53940] 326 326 327 334 335 336 336 337 337 338 ...
##
$ x
: num [1:53940] 3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ...
##
$ y
: num [1:53940] 3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ...
##
$ z
: num [1:53940] 2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
dim(diamonds) finds out how many rows and columns there are
str(diamonds) find out the types of variables per each column
Part B
diamonds2
<-
diamonds
diamonds2
<-
diamonds2[,
!
(
names
(diamonds2)
%in%
c
(
"x"
,
"y"
,
"z"
))]
head
(diamonds2,
3
)
## # A tibble: 3 x 7
##
carat cut
color clarity depth table price
##
<dbl> <ord>
<ord> <ord>
<dbl> <dbl> <int>
## 1
0.23 Ideal
E
SI2
61.5
55
326
## 2
0.21 Premium E
SI1
59.8
61
326
## 3
0.23 Good
E
VS1
56.9
65
327
Part C
frequency
<-
tabyl
(diamonds
$
clarity)
Part D
diamonds2
$
clarity4group
<-
diamonds2
$
clarity
diamonds2
$
clarity4group
<-
ifelse
(diamonds2
$
clarity
%in%
c
(
"I1"
),
"I1"
,
ifelse
(diamonds2
$
clarity
%in%
c
(
"SI2"
,
"SI1"
),
"S"
,
ifelse
(diamonds2
$
clarity
%in%
c
(
"VS2"
,
"VS1"
,
"VVS2"
,
"VVS1"
),
"VS"
,
"
4
diamonds2
$
clarity4group
<-
factor
(diamonds2
$
clarity4group,
levels =
c
(
"I1"
,
"S"
,
"VS"
,
"IF"
),
ordered =
TRUE
)
diamonds2
$
unit_price
<-
diamonds2
$
price
/
diamonds2
$
carat
str
(diamonds2)
## tibble [53,940 x 9] (S3: tbl_df/tbl/data.frame)
##
$ carat
: num [1:53940] 0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
##
$ cut
: Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
##
$ color
: Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ...
##
$ clarity
: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ...
##
$ depth
: num [1:53940] 61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
##
$ table
: num [1:53940] 55 61 65 58 58 57 57 55 61 61 ...
##
$ price
: int [1:53940] 326 326 327 334 335 336 336 337 337 338 ...
##
$ clarity4group: Ord.factor w/ 4 levels "I1"<"S"<"VS"<..: 2 2 3 3 2 3 3 2 3 3 ...
##
$ unit_price
: num [1:53940] 1417 1552 1422 1152 1081 ...
clarity4group_frequency
<-
tabyl
(diamonds2
$
clarity4group)
clarity4group_frequency
##
diamonds2$clarity4group
n
percent
##
I1
741 0.01373749
##
S 22259 0.41266222
##
VS 29150 0.54041528
##
IF
1790 0.03318502
Part E
averages
<-
aggregate
(
cbind
(carat, depth, price, unit_price)
~
cut,
data =
diamonds2,
FUN =
mean)
averages
##
cut
carat
depth
price unit_price
## 1
Fair 1.0461366 64.04168 4358.758
3767.256
## 2
Good 0.8491847 62.36588 3928.864
3860.028
## 3 Very Good 0.8063814 61.81828 3981.760
4014.128
## 4
Premium 0.8919549 61.26467 4584.258
4222.905
## 5
Ideal 0.7028370 61.70940 3457.542
3919.700
5