5.1 Creating Data Frames.......................................................
To begin, let’s take another look at our simple data frame example from
Section 1.4.5:
> kids <- c("Jack","Jill")
> ages <- c(12,10)
> d <- data.frame(kids,ages,stringsAsFactors=FALSE)
> d # matrix-like viewpoint
kids ages
1 Jack 12
2 Jill 10
The first two arguments in the call todata.frame()are clear: We wish to
produce a data frame from our two vectors:kidsandages. However, that
third argument,stringsAsFactors=FALSErequires more comment.
If the named argumentstringsAsFactorsis not specified, then by default,
stringsAsFactorswill beTRUE. (You can also useoptions()to arrange the oppo-
site default.) This means that if we create a data frame from a character
vector—in this case,kids—R will convert that vector to afactor. Because our
work with character data will typically be with vectors rather than factors,
we’ll setstringsAsFactorstoFALSE. We’ll cover factors in Chapter 6.
5.1.1 Accessing Data Frames..........................................
Now that we have a data frame, let’s explore a bit. Sincedis a list, we can
access it as such via component index values or component names:
> d[[1]]
[1] "Jack" "Jill"
> d$kids
[1] "Jack" "Jill"
But we can treat it in a matrix-like fashion as well. For example, we can
view column 1:
> d[,1]
[1] "Jack" "Jill"
This matrix-like quality is also seen when we takedapart usingstr():
> str(d)
'data.frame': 2 obs. of 2 variables:
$ kids: chr "Jack" "Jill"
$ ages: num 12 10
R tells us here thatdconsists of two observations—our two rows—that
store data on two variables—our two columns.
102 Chapter 5