Limited Offer Get 25% off — use code BESTW25
No AI No Plagiarism On-Time Delivery Free Revisions
Claim Now

R language

R language

You need to write one R Markdown file that contains the answers to all of the questions

  • Include comments in your code that explain what you have done and why. Unless specified otherwise,
    include both your code (echo=T) and its output (eval=T) in the PDF.
  • Submit a single zip file, with only two files inside: the .Rmd file and the knitted PDF and/or HTML output. The filenames should be in the form of surname_student number_assignment. Rmd and surname_student number_assignment.pdf or html. e.g. kepler_12345678_assignment.pdf.
  • Ensure that your name and student number appear in each file.
  • Important: complete Question 1 before you continue with the remainder of the assignment.

Question 1: Preliminary (5 marks)

For this question, you will carry out a preliminary submission of the assignment, take a screenshot of the resulting receipt, and include it in your main assignment document. Please follow these steps:

  1. Start a new markdown document with ‘File -> New File -> R Markdown…’ and knit it into a PDF or HTML file.
  2. Submit this PDF or HTML file. 
  3. You will get a submission receipt. Take a screenshot of the receipt.
  4. Start your new R Markdown document for this assignment. Include the screenshot as the start of your answer to this question.
  5. Add a chunk of R code to set variables (student name for your name and student number for your student number).
  6. Add chunks of R code to print out your name and student number, using the chunk options as appropriate:
  • Show the code you’re using, without the results, in the document.
  • Show the evaluated output in the document.
  • Show the code, followed by the evaluated output in the document.

Question 2: Manipulating Arrays (10 marks)

  1. Write a chunk of R code that ensures that your student number is an integer.
  2. Run the following code to seed the random number generator: set.seed (student number).
  3. Create a random vector with 100 elements using the function runif(). Print your vector as per the example that follows (its entries will be different to the one that follows). My vector:

[1] 0.562583 0.750217 0.425158 0.724771 0.761201 0.940472 0.624377
[8] 0.766676 0.440252 0.239865 0.023170 0.562284 0.626722 0.677718
[15] 0.957524 0.323970 0.002627 0.890840 0.608366 0.993184 0.225613
[22] 0.233224 0.207173 0.301462 0.943345 0.350438 0.522908 0.233365
[29] 0.134381 0.867709 0.668160 0.477765 0.335556 0.717439 0.693888
[36] 0.013811 0.760189 0.983373 0.608502 0.735450 0.706588 0.843429
[43] 0.190418 0.312451 0.776279 0.018437 0.165351 0.110034 0.662807
[50] 0.785918 0.023608 0.841467 0.253687 0.164745 0.835321 0.122368
[57] 0.706402 0.645022 0.438843 0.572461 0.083757 0.365065 0.761024
[64] 0.753776 0.417280 0.737796 0.465674 0.011362 0.802228 0.485271
[71] 0.063163 0.738254 0.824773 0.370142 0.661379 0.735624 0.357968
[78] 0.422491 0.034208 0.700918 0.819365 0.691239 0.485995 0.918770
[85] 0.666046 0.053952 0.045951 0.231967 0.158570 0.942530 0.020232
[92] 0.989411 0.780303 0.944438 0.701194 0.436745 0.772391 0.019719
[99] 0.440165 0.538446

  1. Print the following information about the numbers in your vector. Be sure to format the result in the same way as shown:
    • Minimum value: 0.002626505. Maximum value: 0.9931845.
    • Average value: 0.5154095. Median value: 0.5675221.
  2. Choose a number randomly between 4 and 9 using sample(). Call that number n. Print the nth smallest and nth largest value in the vector:
    • The 7th smallest value: 0.02317.
    • The 7th largest value: 0.9425298.
  3. Plot the random data vector (as y) against the range 1 to 100 (as x).
  4. Sort the random data vector into ascending order, and plot the sorted random data vector against
    the range 1 to 100.
  5. Use the linear.regression function from 6.2 Functions 1 to fit a straight line to the data plotted in step 7. Print out the gradient and intercept of the fitted line.

Question 3: Data Exploration (15 marks)

In this question, you will load up the passenger list of the RMS Titanic, including whether or not any given passenger survived its sinking. You will sort the data in a few different ways, and characterise different subsets. Note that many of the following tasks will require multiple steps in data analysis:

  1. Load the text file Titanic (TXT 56 KB) into a data frame, using a name of your choice (don’t use ‘Titanic’ as a variable, since a similar ‘Titanic’ data set is already found in R, but with some differences).
  2. Examine the data file, and write short notes to explain its structure, including any unexpected features.
  3. Calculate the fractions (or percentages) of male and female passengers who survived from 1st, 2nd and 3rd class. Present your results in a table.
  4. Age is given for most passengers. Use the age data to separate male and female children from male and female adults:
    • Rewrite your table to present the survival rates of these four categories from each of the three classes of passenger.
    • Specify how you dealt with passengers without given ages.
  5. Plot a histogram of the ages of all passengers in the list.
  6. Plot histograms of the ages of all male passengers and all female passengers.
  7. Separate female passengers into married and unmarried women.
    • Plot histograms of the ages of all the married and unmarried women.
    • Plot histograms of the ages of married and unmarried women who survived.

Question 4: Throwing dice (10 marks)

This question is about rolling normal (6-sided) dice many times, and looking for ‘runs’ of numbers i.e. the same number coming up more than once in succession:

 Attempt to imitate a sequence of dice rolls:

Create a vector of numbers (1, 2, 3, 4, 5, 6) of length 51. Be as random as you can. Do this before you read the remainder of the question. Here is an example of a sequence: 132654544213651245523511251656545222161524366215433

Can you devise a test to distinguish between the fake dice-rolling and real dice-rolling i.e. a truly random process? One method is based on the observation that people tend to put fewer runs of numbers in their sequences than you find in random sequences:

Write code to count the number of runs of 1 (i.e. each number that is not followed by the same number), 2 (i.e. 2 consecutive numbers the same), 3 and 4.

How many runs of each length would we get if the data were truly random?

    • Write a loop to simulate 105 sets of 51 tosses, storing the number of runs of each length (1, 2, 3, 4).
    • Plot the histogram of runs (i.e. total number of runs of each length added up over all 105 sets).
    • Provide your totals in a table.

The post R language appeared first on My Assignment Online.

Plagiarism Free Assignment Help

Expert Help With This Assignment — On Your Terms

✓ Native UK, USA & Australia writers ✓ Deadline from 3 hours ✓ 100% Plagiarism-Free — Turnitin included ✓ Unlimited free revisions ✓ Free to submit — compare quotes
Scroll to Top