代做ADAN|ADEC7900 Software Tools R Homework 1代做R编程
- 首页 >> CSADAN|ADEC7900 Software Tools
R Homework 1
Objective
This assignment is about learning the basics of R syntax, logic and structure. Your goal will be to write chunks of code that utilize common commands and operators you will be using extensively later in other assignments.
Instructions
1. You should submit this assignment as a single R script. file that produces all the necessary output as required by the questions below. Important: your entire code should run without any errors! Having an error that breaks code execution, even if it is a small typo, will result in loss of credit.
2. Make sure to use the template R script. file from Canvas as a starting point. Use comments inside your file to explain what you are doing with each part of each question (this will help review your work more efficiently). Make sure to comment out any code that is redundant, e.g. View() calls that you maybe using to double check your work.
3. Important: all questions below must be completed using base R functions. Do not use any functions that come from external packages, i.e. packages that are not included in R's default installation.
4. Make sure there is no redundant/unused code in your files. Your scripts should only include your comments and code that must be executed to obtain answers. If you create any temporary objects (variables, datasets, etc), make sure to remove them at the end of your code.
FizzBuzz
In this question you will need to replicate a common group word game for children called "FizzBuzz." Every computer science student loves FizzBuzz.
1. Define a function fizz(n) that takes an integer n and returns a character string "Fizz" if n is divisible by 3 and an empty string "" if it is not.
Use %% operator to calculate a remainder from a division, e.g. 7 %% 5
Hint: Avoid using print() to make your function return the result, use return() instead.
While in some instances both options will be equivalent, in more advanced cases using print() will not work.
2. Define a similar function buzz(n) that takes an integer n and returns a character string "Buzz"
if n is divisible by 5 and an empty string "" if it is not.
3. Use the two functions above to define a function fizzbuzz(n) that takes an integer n and returns one of the following results:
● a character string "Fizz" if n is divisible by 3
● a character string "Buzz" if n is divisible by 5
● a character string "FizzBuzz" if n is divisible by both 3 and 5
● an empty character string “” if none of the above
There should be no conditional statements inside fizzbuzz(n) checking for remainder of
the division (e.g. if (n %% 5 == 0) ). Instead, you should call your previously defined functions fizz(n) and buzz(n) from inside fizzbuzz(n) to evaluate the supplied integer argument.
You can still use conditional statements to check for other things, such as whether the ouput of fizz(n) is equal to "Fizz" or not.
Hint: the whole fizzbuzz(n) code can be written as one simple line using paste() function to combine the outputs of fizz() and buzz() .
4. Use the function fizzbuzz() to define a function fbr(n,m) that takes two integers n and m as two inputs, goes through all integer numbers from n up tom, returning a vector that for
each number between n and m contains either a string "Fizz/Buzz/FizzBuzz" as defined by fizzbuzz(n) or the number itself.
Example: fbr(10,15) should return the following vector:
( ”Buzz” , ”11”, ”Fizz”, ”13”, ”14”, ”FizzBuzz”)
4.1 (Optional, not graded) Make fbr(n,m) check whether supplied arguments are integers (using is.integer() function) and print an error message (using stop() function)
otherwise.
4.2 (Optional, not graded) Make fbr(n,m) check whether n is less or equal tom and print an error message (using stop() function) otherwise.