ps4_tonyzhang

pdf

School

Pennsylvania State University *

*We aren’t endorsed by this school

Course

184

Subject

Computer Science

Date

Apr 3, 2024

Type

pdf

Pages

8

Uploaded by MajorMaskMule13

Report
Assignment Title Your Name Here Date 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 ( "mdsr" , repos = "http://cran.us.r-project.org" ) install.packages ( "dplyr" , repos = "http://cran.us.r-project.org" ) library (mdsr) library (dplyr) data ( "Minneapolis2013" ) same_first_second <- Minneapolis2013 %>% filter (First == Second) num_same_first_second <- nrow (same_first_second) head (same_first_second) top_3_choices <- same_first_second %>% count (First) %>% arrange ( desc (n)) %>% head ( 3 ) top_3_choices ole_savior_first <- Minneapolis2013 %>% filter (First == "Ole Savior" ) top_3_second_choices <- ole_savior_first %>% count (Second) %>% arrange ( desc (n)) %>% head ( 3 ) print (top_3_second_choices) install.packages ( "ggplot2" , repos = "http://cran.us.r-project.org" ) library (ggplot2) data (diamonds) diamonds %>% group_by (color) %>% summarise ( avg_carat = mean (carat)) %>% arrange ( desc (avg_carat)) %>% head ( 1 ) diamonds %>% group_by (clarity) %>% summarise ( avg_table_per_carat = mean (table / carat)) %>% arrange ( desc (avg_table_per_carat)) %>% head ( 1 ) Minneapolis2013 <- Minneapolis2013 %>% group_by (First) %>% mutate ( name_count = n ()) %>% mutate ( First = if_else (name_count < 5000 , "minor" , First)) %>% select ( - name_count) head (Minneapolis2013) 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
Problem 10.1 Part 1 str() - data verb Part 2 group_by() - transformation function Part 3 rank() - summary function Part 4 mean() - reduction function Part 5 filter() - transformation function Part 6 summary() - summary function Part 7 summarise() - summary function Part 8 glimpse() - summary function Problem 10.2 Part 1 mutate() Part 2 arrange() Part 3 filter() 4
Part 4 select() Part 5 group_by(), summarise() Problem 10.3 Part 1 nrow() Part 2 colnames() Part 3 help() Part 4 library() Part 5 group_by() Problem 10.4 Part 1 arrange() Part 2 filter() Part 3 filter() Part 4 summarise() 5
Part 5 select() Problem 10.5 Part 1 I want to use filter() to include only rows where First and Second are the Same install.packages ( "mdsr" , repos = "http://cran.us.r-project.org" ) ## package ’mdsr’ successfully unpacked and MD5 sums checked ## ## The downloaded binary packages are in ## C:\Users\tonyz\AppData\Local\Temp\RtmpU1K5gg\downloaded_packages install.packages ( "dplyr" , repos = "http://cran.us.r-project.org" ) ## package ’dplyr’ successfully unpacked and MD5 sums checked ## ## The downloaded binary packages are in ## C:\Users\tonyz\AppData\Local\Temp\RtmpU1K5gg\downloaded_packages library (mdsr) library (dplyr) data ( "Minneapolis2013" ) same_first_second <- Minneapolis2013 %>% filter (First == Second) num_same_first_second <- nrow (same_first_second) head (same_first_second) ## Precinct First Second Third Ward ## 1 P-04 undervote undervote undervote W-6 ## 2 P-08 undervote undervote undervote W-5 ## 3 P-03 OLE SAVIOR OLE SAVIOR OLE SAVIOR W-6 ## 4 P-02 undervote undervote MARK ANDREW W-6 ## 5 P-02 undervote undervote undervote W-3 ## 6 P-01 BETSY HODGES BETSY HODGES BETSY HODGES W-8 Part 2 I want to count the frequency of each candidate and use sort() to sort the table in descending order and extract the top 3 choices. top_3_choices <- same_first_second %>% count (First) %>% arrange ( desc (n)) %>% head ( 3 ) top_3_choices ## First n ## 1 undervote 686 ## 2 MARK ANDREW 227 ## 3 BETSY HODGES 222 6
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
Part 3 I want to filter the dataset by using filter() where Ole Savior is the first choice. Then I want to count the frequency of each variable in Second and print the top 3 choices. ole_savior_first <- Minneapolis2013 %>% filter (First == "Ole Savior" ) top_3_second_choices <- ole_savior_first %>% count (Second) %>% arrange ( desc (n)) %>% head ( 3 ) print (top_3_second_choices) ## [1] Second n ## <0 rows> (or 0-length row.names) Problem 10.6 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\RtmpU1K5gg\downloaded_packages library (ggplot2) data (diamonds) diamonds %>% group_by (color) %>% summarise ( avg_carat = mean (carat)) %>% arrange ( desc (avg_carat)) %>% head ( 1 ) ## # A tibble: 1 x 2 ## color avg_carat ## <ord> <dbl> ## 1 J 1.16 diamonds %>% group_by (clarity) %>% summarise ( avg_table_per_carat = mean (table / carat)) %>% arrange ( desc (avg_table_per_carat)) %>% head ( 1 ) ## # A tibble: 1 x 2 ## clarity avg_table_per_carat ## <ord> <dbl> ## 1 VVS1 141. Problem 10.7 7
Minneapolis2013 <- Minneapolis2013 %>% group_by (First) %>% mutate ( name_count = n ()) %>% mutate ( First = if_else (name_count < 5000 , "minor" , First)) %>% select ( - name_count) head (Minneapolis2013) ## # A tibble: 6 x 5 ## # Groups: First [3] ## Precinct First Second Third Ward ## <chr> <chr> <chr> <chr> <chr> ## 1 P-10 BETSY HODGES undervote undervote W-7 ## 2 P-06 minor MARK ANDREW undervote W-10 ## 3 P-09 minor BOB FINE MIKE GOULD W-10 ## 4 P-05 BETSY HODGES DON SAMUELS undervote W-13 ## 5 P-01 DON SAMUELS undervote undervote W-5 ## 6 P-04 minor undervote undervote W-6 8