Today's lab on vectors and arrays in the R programming language.

Try R Vector Tutorial

Last lab, we worked through the first chapter of the Try R.

Today, we will work through Chapter 2: Vectors of Try R. Work through the entire chapter on vectors, and then try the vector and plotting below.

Plotting Vector Data

Let's try some of the ideas from the Try R tutorial to plot Vector Data.

Open up the R console (either via Applications folder or Spotlight). At the prompt, create a vector of population for the Bronx:

	bronxPop <- c(200507,430980,732016,1265258,1394711,1451277,1424815,1471701,1168972,1203789,1332650,1385108)
This is the population from 1900, 1910, 1920, ... 2010 that we used in Lab 6.

What does it look like? Let's try plotting it:

	plot(bronxPop)

It's okay, but we're missing the years. Let's create another vector with the years. We have a start of 1900, a stop of 2010, and a step of 10:

	years <- c(seq(1900,2010,10))

Check to make sure we have the right numbers by typing it's name at the prompt:

	years

Now, we can use years as the x-axis and bronxPop as the y-axis:

	plot(years,bronxPop)

To make it prettier, let's add in some color:

	plot(years,bronxPop,col="red")

Let's also try making a barplot:

	barplot(bronxPop)

The labels are missing. barplot() works a bit differently than plot for specifying the labels for the x-axis. We can either give the labels as a parameter:

	barplot(bronxPop, names.arg=c(seq(1900,2010,10)))
Or, we can set the names for bronxPop (as in the Try R tutorial) and then use barPlot:
	names(bronxPop) = c(seq(1900,2010,10))
	barplot(bronxPop)
will yield the same plot.

Some More Useful Vector Commands

In addition to the vector commands in the tutorial, there are several other ones that we will use often to analyze the data.

The first set takes as input a vector and returns a number:

The next set takes a vector as input and also returns a vector:

Here's an example, of the high temperatures in New York City in March 2017:

temps = c(66,63,38,29,35,44,50,59,60,47,28,30,35,32,27,39,47,38,47,51,59,49,43,55,56,42,50,46,58,51,43)
Try the following:
  1. First, use plot() to visualize the data as we did above.
  2. Using the commands above, what is the maximum high temperature for the month?
  3. What is the minimum high temperature for the month?
  4. What is the average (mean) high temperature for the month?
  5. Create a vector of the daily differences between temperatures (i.e. how much the temperature changed day-to-day). Plot the vector.

In-class Quiz

During lab, there is a quiz on R basics. The password to access the quiz will be given during lab.

What's Next?

If you finish the lab early, now is a great time to get a head start on the programming problems due early next week. There's instructors to help you and you already have Python up and running. The Programming Problem List has problem descriptions, suggested reading, and due dates next to each problem.