Reference no: EM132263373
Make an R program to compute the income tax based on the old income tax rate:
Under $25,000 = 15%
$25,000-74,999 = 25%
$75,000-149,999 = 28%
$150,000-299,999 = 33%
$300,000 and over = 35%
The main program creates a random sample incomes between $1,900 to $500,000 of 15 people. The function set.seed(256) is to set the random number seed to 256. The random number generator will generate the sequence of random samples specific to the seed. A fixed seed will allow you getting the SAME random samples for every run. That way, you will have an easier time to debug your program. This, by the way, demonstrates to you that they are not real random samples. They are what is called pseudo-random numbers. The function sample ( ) does the actual work. Check the help manual on sample ( ) to find out more.
The 15 random samples between $1,900 and $500,000 is then assigned to vector incomes. In the next statement:
income.table = income.tax(incomes)
the assignment statement calls a user defined function income.tax( incomes) by passing the incomes vector to that function. Consequently, income.tax ( ) will return a data frame with two columns: incomes and rate of each person. The data frame is assigned to income.table, which allows you compute the tax for each person by multiplying the incomes and rate. Then the mean and standard deviation and the histogram of incomes of this 15 people sample are computed and displayed.
1. Write the income.tax ( ) function that computes and creates the data frame. YOU ARE NOT ALLOWED TO USE LOOPS. (Hints: sapply( ).)
2. After (1) is working, change the sample size from 15 to 150000. Repeat the execution. Compare the histogram of this run to the one you got in 1. Explain your results in a Word document. Save it as prog7.docx and save your R script as incometax.R
Question 2:
This exercise is to make sure you know the basic construct and flow of control of nested if-else statements. I added line number to the LeapYear.R that you did in Prog1 as follows:
1. year = 1900 # This is an inline comment. We assign the integer 1900 to variable name year
2. answer = 'Not a Leap Year' #initialize year, assuming it is not a leap year
3. if (year %% 4 == 0) # if the year is fully divisible by 4
{
4. If ( year %% 100 == 0) # if the year is fully divisible by 100
{
5. if ( year %% 400 == 0) # and year is fully divisible by 400, then it is a leap year
6. answer = 'Leap Year'
} # not a leap year
7. else answer = 'Leap Year' # if it is fully divisible by 4 but not 100, then it is a leap year
}
8. paste(year, answer)
Fill in the execution step (line numbers) in the table below for the following years: 1900, 1992, 2000, 2016, 2018,2019, 2020. I illustrated the execution for year 2018.
Leap Yr = No
Explanations: line 1, year = 2018; line 2, answer = 'Not a Leap Year'; line 3, "if" statement is false. It drops completely from the if-else block (from line 3 to 7) to line 8 since answer remains as 'Not a Leap Year'.