{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern