Time for action - data setup

Next, we need to import our battle data into R and isolate the portion pertaining to past fire attacks:

  1. Copy the battleHistory.csv file into your R working directory. This file contains data from 120 previous battles between the Shu and Wei forces.
  2. Read the contents of battleHistory.csv into an R variable named battleHistory using the read.table(...) command:
    > #read the contents of battleHistory.csv into an R variable
    > #battleHistory contains data from 120 previous battles
    between the Shu and Wei forces
    > battleHistory <- read.table("battleHistory.csv", TRUE, ",")
    
  3. Create a subset using the subset(data, ...) function and save it to a new variable named subsetFire:
    > #use the subset(data, ...) function to create a subset of
    the battleHistory dataset that contains data only from battles
    in which the fire attack strategy was employed
    > subsetFire <- subset(battleHistory, battleHistory$Method ==
    "fire")
    
  4. Verify the contents of the new subset. Note that the console should return 30 rows, all of which contain fire in the Method column:
    > #display the fire attack data subset
    > subsetFire
    
    Time for action - data setup

What just happened?

As we have in previous chapters, we imported our dataset and then created a subset containing our fire attack data. However, this time we used a slightly different function, called read.table(...), to import our external data into R.

read.table(...)

Up to this point, we have always used the read.csv() function to import data into R. However, you should know that there are often many ways to accomplish the same objectives in R. For instance, read.table(...) is a generic data import function that can handle a variety of file types. While it accepts several arguments, the following three are required to properly import a CSV file, like the one containing our battle history data:

  • file: the name of the file to be imported, along with its extension, in quotes
  • header: whether or not the file contains column headings; TRUE for yes, FALSE (default) for no
  • sep: the character used to separate values in the file, in quotes

Using these arguments, we were able to import the data in our battleHistory.csv into R. Since our file contained headings, we used a value of TRUE for the header argument and because it is a comma-separated values file, we used "," for our sep argument:

> battleHistory <- read.table("battleHistory.csv", TRUE, ",")

This is just one example of how a different technique can be used to achieve a similar outcome in R. We will continue to explore new methods in our upcoming activities.

Pop quiz

  1. Suppose you wanted to import the following dataset, named newData into R. Which of the following read.table(...) functions would be best to use?
    4,5
    5,9
    6,12
    

    a. read.table("newData", FALSE, ",")

    b. read.table("newData", TRUE, ",")

    c. read.table("newData.csv", FALSE, ",")

    d. read.table("newData.csv", TRUE, ",")

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.18.65