 Fall 2018

## Programming Structures

The purpose of this project is to introduce you to some standard programming structures: conditionals, loops, and functions.

1. Open R Studio or a text editor and Create a new R file called project3.R. Put your name, Project 3, and the date at the top of the file. No credit will be given for an R file without your name at the top of the file. Create a project3 directory and save your file and R project there. Set your working directory to the source file directory.
2. We're going to continue to use the 1991 General Social Survey and the 2016 GSS data sets. Start by writing the code to read in those data files. Assign the 1991 data to gss91 and the 2016 data to gss16. Test that your code is properly reading the files before continuing.
3. Use conditional indexing to plot Age (X) versus WRKYEARS (Y) for women (SEX == 2) and a second plot for men (SEX == 1). Set the titles and labels so it is clear what is being plotted.
4. Pick a different column or columns and generate a pair of plots (or histograms, or barplots) that use conditional indexing to differentiate the two plots. For example, they might compare different age groups or income brackets.
5. Write a function quadpoly that computes the value of a quadratic polynomial. The parameters of the function should be x, a2, a1, and a0. The function should return a2 times x-squared plus a1 times x plus a0.
6. Write a for loop that executes five times, with the loop variable going from 1 to 5. Each time it loops it should call the quadpoly function and assign the result to a variable. Have your code pass to the quadpoly function the loop variable as x, and 1, 2, 1 as the coefficients. Print out the value of the variable that holds the return value of the function. Your code should generate the numbers 4, 9, 16, 25, and 36.
7. Write a function makeBarPlot. The function should have the following signature.
```makeBarPlot <- function( dataCol, dataTitle="", indexes = NULL, ylim=NULL, xlab=NULL ) {
# body of function goes here
}```

• dataCol - the column of data to plot
• dataTitle - optional main title for the plot
• indexes - optional logical vector for indexing into dataCol
• ylim - optional y-limit argument, should be a 2-element vector
• xlab - optional x axis label

The body of the function should execute the following algorithm.

```  # if indexes is null
#     assign to tmptab the result of calling the table function with dataCol as the argument
# else
#     assign to tmptab the result of calling the table function with dataCol, indexed by indexes, as the argument
#
# assign to tmptab the result of converting tmptab to percentages
#
# assign to tlabels the result of calling names( attr( dataCol, "value.labels" ) )
# assign to tlabels the reverse order of tlabels.  You can use length(tlabels):1 to index backwards
#
# assign to plotObj the result of calling barplot with all the appropriate arguments
# return plotObj```

Call your function with the following line of code in order to test it.

```makeBarPlot( df91\$CHINA, "1991 China Perception (AGE < 40)", df91\$AGE < 40, ylim=c(0,25) )
makeBarPlot( df91\$CHINA, "1991 China Perception (AGE >= 40)", df91\$AGE >= 40, ylim=c(0,25) )
```

8. Select one other column to analyze under two conditions in both the 1991 and 2016 data. Use the makeBarPlot function to create the plots. You should have four calls to the function. If you want a challenge, put this in a loop and analyze multiple columns of data.

### Report

Answer the following questions. Submit your answers as a plain text file or PDF in your handin directory. Put your name at the top of the file. No credit will be given for any other format or for a file without a name.

• What is conditional indexing and why is it useful?
• What is a for loop, and why is it useful?
• What is a function and why is it useful?
• Why do we want to be able to pass information to a function?
• If you assign a value to a variable within a function, does it appear in the global environment?

### Handin

Create a project3 folder inside the Private folder in your Courses directory. Put your project3.R file and your text/PDF file with your answers into the project3 folder. They should be the only documents in the project3 folder. You do not need to put a copy of the GSS data in your handin folder.