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.
- Copy the car MPG data at http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data and paste it into a text file. Save the file as “cars.txt”.
- 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.
- Import the data in “cars.txt” into R with the read.table() command. cars = read.table(“cars.txt”)
- How many rows of data are there? How many variables? Hint: check out the dim() command.
- View the first 10 rows of the cars data.
- 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).
- Bonus: How many rows are there for each value of variable 2?