r3 - Math 127B, Basic R, Lecture 3 Mrinal Raghupathi...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Math 127B, Basic R, Lecture 3 Mrinal Raghupathi Monday, April 11, 2011 Administrivia Today we will learn how to make our own commands and save them for later reuse. We will also learn how to get data in to R and how to use data frames. Reminders 1. There will be a homework assignment sue on Monday in-class. This is the last assignment for the semester. 2. This week’s recitation will again be like a lab for R. Next week’s recitation will be the same. No more quizzes. 1 The cigarette data The programs notepad and textedit are examples of text editors. They allow you to look at plain text files to edit them. The common extension for such a file is .txt . On windows the most common text editor is notepad. On the mac it’s textedit. There are others out there, in fact lots of them. Download the cigarette data to your computer. Either by downloading the file, or by copying and pasting the data into textedit or notepad. The file is called cigarette.dat and you can save it to your desktop or any folder that you like. Next you need to change the working directory to this folder. The command getwd() will show you the current working directory. You can switch directories by either using the command˚ setwd() of by choosing the option from the menu. The cigarette data is arrange into 5 columns: name of brand, tar content (mg), Nicotine content (mg), Weight (g), Carbon monoxide content (mg). The columns are separated by a tab character, which is invisible. Different editors handle tabs in different ways. Tabs are evil, however they didn’t know any better in the good old days! 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
How do we read the data into R? The command is read.table . It accepts a whole bunch of options, but you need to tell it the file you want to read from. Most of the time this will work. cig <- read.table("cigarettes.txt") will store the content of the file in the name cig. Typing cig will show you the data. It looks great, except for the names that have been assigned to the different quantities, they’ve been given unimaginative names like V1,V2 . We can fix this quite easily when we load the file by using the option col.names . We specify a list of names that we want the columns to have. The list I picked was˚ c(”Brandname”, ”Tar”, ”Nicotine”, ”Weight”, ”CO”). So the sequence of commands works nicely > colnames <- c(’Brandname ’, ’Tar ’, ’Nicotine ’ ,’Weight ’, ’CO ’) > cig <- read.table(’cigarettes.txt ’,col.names = colnames)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 10/29/2011 for the course MATH 127B at Vanderbilt.

Page1 / 6

r3 - Math 127B, Basic R, Lecture 3 Mrinal Raghupathi...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online