HW02 — Introduction to R

The tasks (steps) within each of the following homework assignments (A to C) are expected to be performed in the order stated, since an earlier task will often generate the input for the next task.

A. Object Subsetting, Import and Export

  • Task 1: Sort the rows of the iris data frame by its first column and sort its columns alphanumerically by column names.
  • Task 2: Subset the first 12 rows of the sorted iris dataset, export the result to a tabular file and view it in a spreadsheet program like Excel or Google Sheets.
  • Task 3: Change some column titles in your spreadsheet program, save the result to a tab delimited text file and import it back into R. Note, for this task you only want to include the read.table command in the homework result (here R script).

Before you start it can be helpful to evaluate the structure of the iris data set with the following commands:

class(iris)
dim(iris)
colnames(iris)

What to submit: Three R expressions - (1) the sort/subset command(s) applied to the iris object, (2) the write.table() call saving the modified iris object, and (3) the read.delim() call re-importing the modified file.

B. Scatter Plots

  • Task 1: Generate a scatter plot for the first two columns of the iris data frame and color the dots by the Species column.
  • Task 2: Use the xlim/ylim arguments to set limits on the x- and y-axes so that all data points are restricted to the bottom left quadrant of the plot.

Again before you start, evaluate the structure of iris data set. The following commands are useful:

iris[1:4,]
table(iris$Species)

What to submit: Two plot() calls - the first producing a basic colored scatter plot, the second adding axis limits via xlim and ylim.

C. Bar Plots

  • Task 1: Calculate the mean values for the Species components of the first four columns in the iris data frame. Organize the results in a matrix where the row names are the unique values from the iris Species column and the column names are the names of the first four iris columns.
  • Task 2: Generate two bar plots for the matrix generated in the previous step: one with stacked bars and one with horizontally arranged bars.

What to submit: The aggregate() expression (or sapply/tapply alternative) for obtaining the mean value summary as instructed, followed by two barplot() calls.

D-H. Analysis Worflow

The instructions for these homework assignments are here.

What to submit for D–H: The code (expressions) solving the assigned problems given under items D-H of the analysis routine.

Homework Submission

Assemble all code for assignments A–H into a single R script named HW2.R and upload it to your private GitHub repository under Homework/HW2/HW2.R. Note that the preassembled workflow script provided for HW2H (here) does not include solutions for HW2A-H; those must be written by you.

Due Date

Most homeworks will be due one week after they are assigned. This one is due on Thu, April 16th at 6:00 PM.

Homework Solutions

To be posted after due date.

Back to top