lab1.pdf - Lab 1 Introduction to R and Data Management DUE September 7 Some Initial R Basics You can use R to do basic math commands Try doing the

lab1.pdf - Lab 1 Introduction to R and Data Management DUE...

This preview shows page 1 - 3 out of 6 pages.

Lab 1: Introduction to R and Data Management DUE September 7 Some Initial R Basics You can use R to do basic math commands. Try doing the following commands line by line. 1 + 3 # addition 2 * 4 # multiplication 10 / 2 # division 3 ^ 2 # exponentiation Notice that we have some text describing what we’re doing after a “#” sign. This is called commenting your code, and the “#” tells R to ignore everything on the line that comes afterwards. This is useful for keeping notes on what you do in R. Objects R is an object-oriented language. This means that the language is centered around objects rather than actions. Consider the following code. a <- 3 The “ <- ” symbols assign the value of 3 to “ a ” (notice that it looks like an arrow, showing the assignment). Now, we have an object “ a ” that contains the value of 3. Now try running the following code. What do you think will happen? a + 2 Vectors In R, we will work with vectors. Vectors are an ordered collection of a certain type of object. You can use the “ c() ” function to create vectors. numvector <- c ( 1 , 4 , 6 , 7 ) numvector Recall that the assignment in the first line doesn’t actually output anything – it just does the assignment. In order to see what was put into “ numvector ”, we need to run the second line. To make a vector of sequential numbers, we can use a colon. 1 : 10 # This is a vector from 1 to 10. We can do this with other types of variables, like strings. Strings are just text data, which we can make by using quotation marks. stringvector <- c ( "test" , "strings" , "this is some text" ) stringvector Finally, to access different elements of a vector, you can use square brackets. Note that R starts indexing at 1, NOT at 0. stringvector[ 2 ] # This is the second element of stringvector 1
Image of page 1

Subscribe to view the full document.

Exploring Email Data The textbook for this course has an accompanying R package called openintro that has all the data sets used in the examples. Packages, or libraries, in R are basically bundles of useful tools that other people have created for you to download and use. We can use the install.packages() function to install it on our computers. # If you haven t installed the openintro package yet, do so first! install.packages ( openintro ) To load the package that we just installed, we can use either the require() or library() function. require (openintro) This needs to be done every session (that is, every time you open up R again) if you want to use the tools inside the openintro package. Typically, the first thing you put in an R script is a list of packages you want to bring in.
Image of page 2
Image of page 3
  • Spring '17

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

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes