代做GSND 5345Q, Fundamentals of Data Science Homework 4代写留学生R语言

Homework 4

GSND 5345Q, Fundamentals of Data Science

Due Wednesday, February 19th, 2025

Now its time to practice what we have learned in class and learn even more! Note that from now on your homework should be written in R Markdown. Turn in your html file and Rmd files, and any other relevant files in a tarball. Then turn it in by uploading to canvas.

Data Structures (20 points)

1. (10 points) Learn more about the scan, readLines, read html, readr, and readxl functions for getting data into R. Use these functions to read data from a files into a tibble using R. You can use your own data example or a dataset used in class. How are these different from the functions we learned in class? Report what you find and give some examples!

2. (10 points) Learn about the S3, S4, and R6 classes in R. When do you think you would use these? Describe what you learned and give some examples!

R Markdown (30 points; +20 points extra credit)

1. (20 points EXTRA CREDIT) Complete the markdown tutorial at https://www.markdowntutorial.com/. Confirm here that you completed it (on your honor!).

2. (30 points) R markdown is a powerful tool for literate programming.  To gain more practice, as part recreate the  .Rmd file for the example file in the homework folder: “Rmarkdown_example.html.”

Tidyverse (50 points; 2 points each)

These exercises will give you some introductory experience with the tidyverse.  Please complete the following:

1. Examine the built-in dataset co2. Which of the following is true:

a.  co2 is tidy data: it has one year for each row.

b.  co2 is not tidy: we need at least one column with a character vector.

c.  co2 is not tidy: it is a matrix instead of a data frame.

d.  co2 is not tidy: to be tidy we would have to wrangle it to have three columns (year, month and value), then each co2 observation would have a row.

2. Examine the built-in dataset ChickWeight. Which of the following is true:

a.  ChickWeight is not tidy: each chick has more than one row.

b.  ChickWeight is tidy: each observation (a weight) is represented by one row. The chick from which this measurement came is one of the variables.

c.  ChickWeight is not tidy: we are missing the year column.

d.  ChickWeight is tidy: it is stored in a data frame.

3. Examine the built-in dataset BOD. Which of the following is true:

a.  BOD is not tidy: it only has six rows.

b. BOD is not tidy: the first column is just an index.

c.  BOD is tidy: each row is an observation with two values (time and demand)

d. BOD is tidy: all small datasets are tidy by definition.

4. Which of the following built-in datasets is tidy (you can pick more than one):

a. BJsales

b. EuStockMarkets

c. DNase

d. Formaldehyde

e.  Orange

f. UCBAdmissions

5. Load the dplyr package and the murders dataset.

library (dplyr)

library (dslabs)

data (murders)

You can add columns using the dplyr function mutate.  This function is aware of the column names and inside the function you can call them unquoted:

murders  <- mutate (murders,  population_in_millions  =  population / 10 ˆ6)

We can write population rather than murders$population.  The function mutate knows we are grabbing columns from murders.

Use the function mutate to add a murders column named rate with the per 100,000 murder rate as in the example code above. Make sure you redefine murders as done in the example code above ( murders <- [your code]) so we can keep using this variable.

6. If rank(x) gives you the ranks of x from lowest to highest, rank(-x) gives you the ranks from highest to lowest. Use the function mutate to add a column rank containing the rank, from highest to lowest murder rate. Make sure you redefine murders so we can keep using this variable.

7. With dplyr, we can use select to show only certain columns. For example, with this code we would only show the states and population sizes:

select (murders,  state,  population) %>% head ()

Use select to show the state names and abbreviations in murders. Do not redefine murders, just show the results.

8. The dplyr function filter is used to choose specific rows of the data frame to keep.  Unlike select which is for columns, filter is for rows. For example, you can show just the New York row like this:

filter (murders,  state == "New  York")

You can use other logical vectors to filter rows.

Use filter to show the top 5 states with the highest murder rates.  After we add murder rate and rank, do not change the murders dataset, just show the result. Remember that you can filter based on the rank column.

9. We can remove rows using the  != operator. For example, to remove Florida, we would do this:

no_florida  <- filter (murders,  state != "Florida")

Create a new data frame called no_south that removes states from the South region. How many states are in this category? You can use the function nrow for this.

10. We can also use %in% to filter with dplyr. You can therefore see the data from New York and Texas like this:

filter (murders,  state %in% c("New  York" ,  "Texas"))

Create a new data frame called murders_nw with only the states from the Northeast and the West.  How many states are in this category?

11. Suppose you want to live in the Northeast or West and want the murder rate to be less than 1. We want  to see the data for the states satisfying these options. Note that you can use logical operators with filter. Here is an example in which we filter to keep only small states in the Northeast region.

filter (murders,  population < 5000000 & region == "Northeast")

Make sure murders has been defined with rate and rank and still has all states.  Create a table called my_states that contains rows for states satisfying both the conditions: it is in the Northeast or West and the murder rate is less than 1. Use select to show only the state name, the rate, and the rank.

12.  The pipe %>% can be used to perform operations sequentially without having to define intermediate objects. Start by redefining murder to include rate and rank.

murders  <- mutate (murders,  rate  =   total / population * 100000 , rank  = rank (-rate))

In the solution to the previous exercise, we did the following:

my_states  <- filter (murders,  region %in% c("Northeast" ,  "West") & rate < 1)

select (my_states,  state,  rate,  rank)

The pipe %>% permits us to perform both operations sequentially without having to define an intermediate variable my_states. We therefore could have mutated and selected in the same line like this:

mutate (murders,  rate  =   total / population * 100000 ,

rank  = rank (-rate)) %>% select (state,  rate,  rank)

Notice that select no longer has a data frame as the first argument. The first argument is assumed to be the result of the operation conducted right before the %>%.

Repeat the previous exercise, but now instead of creating a new object, show the result and only include the state, rate, and rank columns. Use a pipe %>% to do this in just one line.

13.  Reset murders to the original table by using data(murders).  Use a pipe to create a new data frame called my_states that considers only states in the Northeast or West which have a murder rate lower than 1, and contains only the state, rate and rank columns. The pipe should also have four components separated by three %>%. The code should look something like this:

my_states  <- murders %>%

mutate  SOMETHING %>%

filter  SOMETHING %>%

select  SOMETHING

For exercises 14-20, we will be using the data from the survey collected by the United States National Center for Health Statistics (NCHS). This center has conducted a series of health and nutrition surveys since the 1960’s. Starting in 1999, about 5,000 individuals of all ages have been interviewed every year and they complete the health examination component of the survey. Part of the data is made available via the NHANES package. Once you install the NHANES package, you can load the data like this:

library(NHANES)

data(NHANES)

The NHANES data has many missing values. The mean and sd functions in R will return NA if any of the entries of the input vector is an NA. Here is an example:

library (dslabs)

data (na_example)

mean (na_example)

##  [1]  NA

sd (na_example)

##  [1]  NA

To ignore the NAs we can use the na .rm argument:

mean (na_example,  na .rm  =  TRUE)

##  [1]  2 .301754

sd (na_example,  na .rm  =  TRUE)

##  [1]  1 .22338

Let’s now explore the NHANES data.

14.  We will provide some basic facts about blood pressure.  First let’s select a group to set the standard. We will use 20-to-29-year-old females.  AgeDecade is a categorical variable with these ages. Note that the  category is coded like ” 20-29”, with a space in front! What is the average and standard deviation of systolic  blood pressure as saved in the BPSysAve variable? Save it to a variable called ref.

Hint: Use filter and summarize and use the na.rm  =  TRUE argument when computing the average and standard deviation. You can also filter the NA values using filter.

15. Using a pipe, assign the average to a numeric variable ref_avg. Hint: Use the code similar to above and then pull.

16. Now report the min and max values for the same group.

17. Compute the average and standard deviation for females, but for each age group separately rather than a selected decade as in question 1. Note that the age groups are defined by AgeDecade. Hint: rather than

filtering by age and gender, filter by Gender and then use group_by.

18. Repeat exercise 4 for males.

19.   We can actually combine both summaries for exercises 4 and 5 into one line of code.   This is be- cause group_by permits us to group by more than one variable.   Obtain one big summary table using group_by(AgeDecade,  Gender).

20. For males between the ages of 40-49, compare systolic blood pressure across race as reported in the Race1 variable. Order the resulting table from lowest to highest average systolic blood pressure.

21. Load the murders dataset. Which of the following is true?

a. murders is in tidy format and is stored in a tibble.

b. murders is in tidy format and is stored in a data frame. c. murders is not in tidy format and is stored in a tibble.

d. murders is not in tidy format and is stored in a data frame.

22.   Use  as_tibble  to  convert  the  murders  data  table  into  a  tibble  and  save  it  in  an  object  called murders_tibble.

23. Use the group_by function to convert murders into a tibble that is grouped by region.

24. Write tidyverse code that is equivalent to this code:

exp (mean (log(murders$population)))

Write it using the pipe so that each function is called without arguments. Use the dot operator to access the population. Hint: The code should start with murders  %>%.

25. Use the map_df to create a data frame with three columns named n, s_n, and s n 2. The first column should contain the numbers 1 through 100. The second and third columns should each contain the sum of 1 through n with n the row number.

R Packages and Shiny (50 points)

1.  (20 points) Walk step by step through the K means shiny app tutorial: “shiny_kmeans_app.html”. Run the code to launch the app at each step. Document what changes are made in each step (which functions are used, what they do in the app).

2.  (20 points) Now, make a K means R package with at least two functions (e.g., one that calculates the clusters, one that plots the result). Make sure to document your functions and create unit tests. Change the shiny app to rely on your functions, and add the shiny app to your inst/ directory in your package.

3.  (10 points) Post your R package as a repository on GitHub. Please add a hyperlink to your repository to your RMarkdown here.





热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图