Next, we need to import our battle data into R and isolate the portion pertaining to past fire attacks:
battleHistory.csv
file into your R working directory. This file contains data from 120 previous battles between the Shu and Wei forces. 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, ",")
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")
fire
in the Method
column:> #display the fire attack data subset > subsetFire
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.
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:
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.
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, ",")