本次英国作业案例分享是一个R统计代写的assignment
Prelimiaries
The message function
We can use to the message function to print texts on the screen. The message function takes a character object which is the message that we want to print. For example, if we want to print the classic message,“Hello world!”, then we would use the following command.
message(“Hello world!”)
The readline function
We can use the readline function to prompt the user to provide an input at the command line.
• The readline function takes a character object as an argument.
• After running an R command with readline, the character argument will be printed on the screen as a message.
• After the message, the user can enter some input, and this input is returned by readline as a character object.
For example, if we run the following command in R
input = readline(“Please type something: “)
the following will appear on the screen
Please type something:
Additionally, the computer will wait for us to type something after Please type something: .
Say, we enter English breakfast (and then press the ‘Enter’/‘Return’ key on our keyboard)
input = readline(“Please type something: “)
> Please type something: English breakfast
Then, English breakfast will be returned by readline and assigned to the object input. So if we print out the input object, then we get the following on the screen:
input
[1] “English breakfast”
The as.numeric function
If a character object represents a numeric value, e.g., “3.1412159”, then we can convert that to a numeric object, by using the as.numeric function.
Exercises
Question 1
Print the message, ‘Sorry we cannot understand.’, on the screen using the message function.
Hint: see the section ‘The message function’ under ‘Preliminaries’.
Question 2
Write an R command using the readline function to
• prompt the user with the request ‘Please enter either yes or no: ’ on the screen, and
• assign the user input to an object called answer.
he section ‘The readline function’ under ‘Preliminaries’
Question 3
You have been asked by a slightly fishy delivery company to write an R program to deal with their online enquires. Also, they have specified exactly how they want to respond to various different enquiries. In this question, we will build this online enquiry program step-by-step.
(a) Create a function called procTrackingCode, which takes no argument and does not return any objects.
The procTrackingCode function performs the following tasks.
(i) Firstly, the function will prompt the customer with the request,Please provide the tracking code of your package: ’
(ii) After the customer enters their answer, the answer is assigned to an object called code.
(iii) Because readline returns a character object, so currently the code object is a character object.
Use the as.numeric function to convert code to a numeric object. Hint: see the section The as.numeric functionúnder ‘Preliminaries’.
(iv) Use an appropriate conditional statement to decide the next action, depending on the value of the numeric object code. The details of the decision process is outlined below.
• If code is NA, then use the message function to print the message:
‘Sorry we cannot help without a valid tracking code.’
Hint: The R command is.na(code) returns TRUE if code is NA.