Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Data Analysis with R

You're reading from  Data Analysis with R

Product type Book
Published in Dec 2015
Publisher
ISBN-13 9781785288142
Pages 388 pages
Edition 1st Edition
Languages
Toc

Table of Contents (20) Chapters close

Data Analysis with R
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
1. RefresheR 2. The Shape of Data 3. Describing Relationships 4. Probability 5. Using Data to Reason About the World 6. Testing Hypotheses 7. Bayesian Methods 8. Predicting Continuous Variables 9. Predicting Categorical Variables 10. Sources of Data 11. Dealing with Messy Data 12. Dealing with Large Data 13. Reproducibility and Best Practices Index

Loading data into R


Thus far, we've only been entering data directly into the interactive R console. For any data set of non-trivial size this is, obviously, an intractable solution. Fortunately for us, R has a robust suite of functions for reading data directly from external files.

Go ahead, and create a file on your hard disk called favorites.txt that looks like this:

flavor,number
pistachio,6
mint chocolate chip,7
vanilla,5
chocolate,10
strawberry,2
neopolitan,4

This data represents the number of students in a class that prefer a particular flavor of soy ice cream. We can read the file into a variable called favs as follows:

  > favs <- read.table("favorites.txt", sep=",", header=TRUE)

If you get an error that there is no such file or directory, give R the full path name to your data set or, alternatively, run the following command:

  > favs <- read.table(file.choose(), sep=",", header=TRUE)

The preceding command brings up an open file dialog for letting you navigate to the file you've just created.

The argument sep="," tells R that each data element in a row is separated by a comma. Other common data formats have values separated by tabs and pipes ("|"). The value of sep should then be "\t" and "|", respectively.

The argument header=TRUE tells R that the first row of the file should be interpreted as the names of the columns. Remember, you can enter ?read.table at the console to learn more about these options.

Reading from files in this comma-separated-values format (usually with the .csv file extension) is so common that R has a more specific function just for it. The preceding data import expression can be best written simply as:

  > favs <- read.csv("favorites.txt")

Now, we have all the data in the file held in a variable of class data.frame. A data frame can be thought of as a rectangular array of data that you might see in a spreadsheet application. In this way, a data frame can also be thought of as a matrix; indeed, we can use matrix-style indexing to extract elements from it. A data frame differs from a matrix, though, in that a data frame may have columns of differing types. For example, whereas a matrix would only allow one of these types, the data set we just loaded contains character data in its first column, and numeric data in its second column.

Let's check out what we have by using the head() command, which will show us the first few lines of a data frame:

  > head(favs)
                 flavor number
  1           pistachio      6
  2 mint chocolate chip      7
  3             vanilla      5
  4           chocolate     10
  5          strawberry      2
  6          neopolitan      4

  > class(favs)
  [1] "data.frame"
  > class(favs$flavor)
  [1] "factor"
  > class(favs$number)
  [1] "numeric"

I lied, ok! So what?! Technically, flavor is a factor data type, not a character type.

We haven't seen factors yet, but the idea behind them is really simple. Essentially, factors are codings for categorical variables, which are variables that take on one of a finite number of categories—think {"high", "medium", and "low"} or {"control", "experimental"}.

Though factors are extremely useful in statistical modeling in R, the fact that R, by default, automatically interprets a column from the data read from disk as a type factor if it contains characters, is something that trips up novices and seasoned useRs alike. Because of this, we will primarily prevent this behavior manually by adding the stringsAsFactors optional keyword argument to the read.* commands:

  > favs <- read.csv("favorites.txt", stringsAsFactors=FALSE)
  > class(favs$flavor)
  [1] "character"

Much better, for now! If you'd like to make this behavior the new default, read the ?options manual page. We can always convert to factors later on if we need to!

If you haven't noticed already, I've snuck a new operator on you—$, the extract operator. This is the most popular way to extract attributes (or columns) from a data frame. You can also use double square brackets ([[ and ]]) to do this.

These are both in addition to the canonical matrix indexing option. The following three statements are thus, in this context, functionally identical:

  > favs$flavor
  [1] "pistachio"           "mint chocolate chip" "vanilla"            
  [4] "chocolate"           "strawberry"          "neopolitan"         
  > favs[["flavor"]]
  [1] "pistachio"           "mint chocolate chip" "vanilla"            
  [4] "chocolate"           "strawberry"          "neopolitan"         
  > favs[,1]
  [1] "pistachio"           "mint chocolate chip" "vanilla"            
  [4] "chocolate"           "strawberry"          "neopolitan"  

Note

Notice how R has now printed another number in square brackets—besides [1]—along with our output. This is to show us that chocolate is the fourth element of the vector that was returned from the extraction.

You can use the names() function to get a list of the columns available in a data frame. You can even reassign names using the same:

  > names(favs)
  [1] "flavor" "number"
  > names(favs)[1] <- "flav"
  > names(favs)
  [1] "flav"   "number"

Lastly, we can get a compact display of the structure of a data frame by using the str() function on it:

  > str(favs)
  'data.frame': 6 obs. of  2 variables:
   $ flav  : chr  "pistachio" "mint chocolate chip" "vanilla" "chocolate" ...
   $ number: num  6 7 5 10 2 4

Actually, you can use this function on any R structure—the property of functions that change their behavior based on the type of input is called polymorphism.

You have been reading a chapter from
Data Analysis with R
Published in: Dec 2015 Publisher: ISBN-13: 9781785288142
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime