Stat programming workshop – week 2 tasks

April 2013 update: this post is part 2 of 6 that were designed to help beginning R programmers get up and running with some simple data analyses. They were originally private for a specific course in Summer 2012, but they’re now public in case the tips might be useful for a broader audience. -Brian

Hopefully after working through last week’s tasks you have installed R, can open R, and can look up help files for new commands. This week’s tasks are intended to lead you through importing a data set and computing some basic summary statistics.
– Brian

  1. Copy the car MPG data at and paste it into a text file. Save the file as “cars.txt”.
  2. Open R and change the working directory to the location where “cars.txt” is saved. Hint: the setwd() is what you want for this, and the getwd() command can be used to check that you’re in the right directory. List the files in the directory with the dir() command to make sure “cars.txt” is there.
  3. Import the data in “cars.txt” into R with the read.table() command. cars = read.table(“cars.txt”)
  4. How many rows of data are there? How many variables? Hint: check out the dim() command.
  5. View the first 10 rows of the cars data.
  6. The second variable (called “V2” unless you’ve already added column names) has integer values. Figure out what the unique values are for this variable. Hint 1: check out the unique() command. Hint 2: to access an individual column of our data set use the $ operator. For example, to get the unique values of variable 8, you could do unique(cars$V8).
  7. Bonus: How many rows are there for each value of variable 2?
This entry was posted in teaching. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s