• No results found

Accessing data in data frames

The study data frame has three variables. As before, these can be accessed individually after attaching the data frame to your R session with the attachcommand:

> study

weight height gender

1 150 65 Fe

2 135 61 Fe

3 210 70 M

4 140 65 Fe

> rm(weight) # clean out an old copy > weight

Error: Object "weight" not found > attach(study)

> weight

[1] 150 135 210 140

However, attaching and detaching the data frame can be a chore if you want to access the data only once. Besides, if you attach the data frame, you can’t readily make changes to the original data frame.

To access the data it helps to know that data frames can be thought of as lists or as arrays and accessed accordingly.

To access as an array An array is a way of storing data so that it can be accessed with a row and column. Like a spreadsheet, only technically the entries must all be of the same type and one can have more than rows and columns.

Data frames are arrays as they have columns which are the variables and rows which are for the experimental unit. Thus we can access the data by specifying a row and a column. To access an array we use single brackets ([row,column]). In general there is a row and column we can access. By letting one be blank, we get the entire row or column. As an example these will get the weight variable

> study[,’weight’] # all rows, just the weight column [1] 150 135 210 140

> study[,1] # all rows, just the first column

Array access allows us much more flexibility though. We can get both the weight and height by taking the first and second columns at once

> study[,1:2] weight height Mary 150 65 Alice 135 61 Bob 210 70 Judy 140 65

Or, we can get all of Mary’s info by looking just at her row or just her weight if desired

> study[’Mary’,]

weight height gender Mary 150 65 Fe > study[’Mary’,’weight’] [1] 150

To access as a list A list is a more general storage concept than a data frame. A list is a set of objects, each of which can be any other object. A data frame is a list, where the objects are the columns as vectors.

To access a list, one uses either a dollar sign, $, or double brackets and a number or name. So for our studyvariable we can access the weight (the first column) as a list all of these ways

> study$weight # using $ [1] 150 135 210 140

> study[[’weight’]] # using the name.

> study[[’w’]] # unambiguous shortcuts are okay > study[[1]] # by position

These two can be combined as in this example. To get just the females information. These are the rows where gender is ’Fe’ so we can do this

> study[study$gender == ’Fe’, ] # use $ to access gender via a list weight height gender

Mary 150 65 Fe

Alice 135 61 Fe

Judy 140 65 Fe

Manipulating data frames:

stack

and

unstack

In some instances, there are two different ways to store data. The data setPlantGrowthlooks like

> data(PlantGrowth) > PlantGrowth weight group 1 4.17 ctrl 2 5.58 ctrl 3 5.18 ctrl 4 6.11 ctrl ...

There are 3 groups a control and two treatments. For each group, weights are recorded. The data is generated this way, by recording a weight and group for each plant. However, you may want to plot boxplots for the data broken down by their group. How to do this?

A brute force way is do as follows for each value of group > attach(PlantGrowth)

> weight.ctrl = weight[group == "ctrl"]

This quickly grows tiresome. The unstack function will do this all at once for us. If the data is structured correctly, it will create a data frame with variables corresponding to the levels of the factor.

> unstack(PlantGrowth) ctrl trt1 trt2 1 4.17 4.81 6.31 2 5.58 4.17 5.12 3 5.18 4.41 5.54 4 6.11 3.59 5.50 ...

Thus, to do a boxplot of the three groups, one could use this command

> boxplot(unstack(PlantGrowth))

Using

R’s model formula notation

The model formula notationthat R uses allows this to be done in a systematic manner. It is a bit confusing to learn, but this flexible notation is used by most of R’s more advanced functions.

To illustrate, the above could be done by (if the data framePlantGrowth is attached)

> boxplot(weight ~ group)

What does this do? It breaks the weight variable down by values of the group factor and hands this off to the boxplot command. One should read the line weight ∼ groupas “model weight by the variable group”. That is, break weight down by the values of group.

When there are two variables involved things are pretty straightforward. The response variable is on the left hand side and the predictor on the right:

response ∼ predictor (when two variables).

When there are more than two predictor variables things get a little confusing. In particular, the usual mathematical operators do not do what you may think. Here are a few different possibilities that will suffice for these notes.9

Suppose the variables are generically named Y, X1, X2

9

A thorough explanation of the syntax and its usage is found in the manual “An Introduction toR” which accompanies the R software, and the contributed document “Using R for Data Analysis and Graphics” by Maindonald. See the appendix for more information on these.

formula meaning

Y ∼ X1 Y is modeled by X1

Y ∼ X1 + X2 Y is modeled by X1and X2as in multiple regression

Y ∼ X1 * X2 Y is modeled by X1, X2and X1*X2

(Y ∼ (X1 + X2)ˆ2) Two-way interactions. Note usual powers Y ∼ X1+ I((X2^2) Y is modeled by X1and X22

Y ∼ X1 | X2 Y is modeled by X1conditioned on X2

The exact interpretation of “modeled by” varies depending upon the usage. For theboxplotcommand it is different than the lmcommand. Also notice that usual mathematical meanings are available, but need to be included inside the I function.

Related documents