Solutions_Assignment #1_Max 503

docx

School

Illinois Institute Of Technology *

*We aren’t endorsed by this school

Course

503

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

7

Uploaded by DukeFlagJaguar44

Report
Max 503 Assignment #1 Instructions: Note 1: there may be many ways to solve a problem in R. As we progress, we will demonstrate progressively better ways to solve some of the same problems. R programmers may differ as to what constitutes “better.” Some may prefer elegance while others prefer speed or ease of comprehension. At this point, I recommend that you consider whether a solution seems optimal, but don’t worry too much about it. Getting a correct answer in any one of multiple possible ways is the most important outcome. Note 2: When an analysis includes more than a few simple steps, it becomes difficult to remember what you’ve done and even more difficult for others to understand it. In the worst case, you might perform analyses with undocumented console commands or out- of-sequence commands run from a code file. In that case, the code file doesn’t reflect the true sequence, and the results may be non-reproducible. If you run the code again, you may get a different answer because the steps are in a different order. Luckily, the R community has demonstrated leadership in addressing these issues and provided many powerful tools that can help. R Notebooks is one of those tools. R Notebooks Offered in RStudio, R Notebooks provide R Markdown live inside the RStudio environment. A notebook includes any text you want to write, along with R code and the *results* of R code. When you execute R code in a notebook, the results are shown inside your document. Here are a few tips: o You can create Section headers by using 1, 2, or 3 number signs (#) at the beginning of a line, followed by text. For example: #1 R code ………… #2 R code ………… (you follow the sequence like this) o You can format text with asterixes. Use *one for italic*, or **two for bold**. 1
Max 503 Assignment #1 o You can include R code by clicking the *Insert Chunk* button on the toolbar. Put your code between the lines with "```". A Basic R Notebook To create an R Notebook, launch RStudio Select File New File R Notebook The default R Notebook template will appear as an untitled document, as shown in the following figure: Note a few features of notebooks. At the top is a section with document settings, demarcated with three dashes, specifying the title (“R Notebook”, which you may edit ) and the targeted output format (HTML). Below that, there are two paragraphs of text, followed by an R code chunk with a gray background. 2
Max 503 Assignment #1 The R code chunk may contain any R commands, and they will be executed when the green “Run” (triangular) icon is clicked (or choose Code—Run Region—Run Current Chunk from the menu or shortcut keys). When we run the chunk in the default notebook, it plots the cars data set and shows the result inline in RStudio, as shown in the following figure : Inline plot shown in the notebook by clicking the green “Run current chunk” icon 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
Max 503 Assignment #1 Saving and Sharing Your Results: Click the *Preview* button to see your document formatted as an output file. Be sure to run all your code first because Preview doesn't do that. *Run | Run All* will do that. o One way to share your work is to email the .Rmd (R markdown) file for your notebook. o Another option is to publish the notebook online (search for "publish Rstudio notebook"). Those allow the recipient to run your code and check it. You can also save just the output -- with code, results, and plots -- in several formats. o Click on the *Preview* button above the code pane. o You will see options to *Knit to HTML* (useful for saving results to a web site) *Knit to PDF* (create a PDF) *Knit to Word* (save it as a Word document) For classroom usage (assignments) always share your notebook in PDF format . This can be done by selecting Knit to PDF from the Preview button at the top of the RStudio editing window. An example of the output is shown here … 4
Max 503 Assignment #1 Note that all of the options for reproducible formats—notebooks, HTML, Word format, and PDF —may require installation of additional packages the first time you create such documents. RStudio should install the packages automatically. Or try this: tinytex::reinstall_tinytex() If your output does not appear the first time, try knitting the output again after package installation. 5
Max 503 Assignment #1 1. Create a text vector called Months with names of the 12 months of the year. Months <- c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") 2. Create a numeric vector Summer, with Calendar month index positions for the summer months (inclusive, with 4 elements in all). Summer <- 6:9 3. Use vector indexing to extract the text values of Months, indexed by Summer. Months [Summer] 4. Multiply Summer by 3. What are the values of Months , when indexed by Summer multiplied by 3? Why do you get that answer? Summer * 3 Months [Summer * 3] 5. What is the mean(average)summer month, as an integer value? Which value of Months corresponds to it? Why do you get that answer? mean(Summer) Months[mean(Summer)] 6. Use the floor() and ceiling() functions to return the upper and lower limits of Months for the average Summer month. (Hint: to find out how a function works, use R help if needed.) Months[floor(mean(Summer))] "July" Months[ceiling(mean(Summer))] 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
Max 503 Assignment #1 "August" 7. Using the store.df data from Week 3 lecture, how many visits did Bert’s store have? store.num <- factor(c(3, 14, 21, 32, 54)) # store id store.rev <- c(543, 654, 345, 678, 234) # store revenue, $1000 store.visits <- c(45, 78, 32, 56, 34) # visits, 1000s store.manager <- c("Annie", "Bert", "Carla", "Dave", "Ella") (store.df <- data.frame(store.num, store.rev, store.visits, store.manager, stringsAsFactors=F)) store.df[2 , 3] # better answers in future lectures 78 8. It is easy to make mistakes in indexing. How can you confirm that the previous answer is actually from Bert’s store? Show this with a command that produces no more than 1 row of console output. store.df[2, ] # better answers in future lectures store.num store.rev store.visits store.manager 2 14 654 78 Bert 7