next up previous
Next: Attaching a Data Frame Up: Reading Data in S-PLUS Previous: Example 5

Referencing Data in a Data Frame

We could have stored the example data in three different columns, instead of a single data frame. However, that would have created three times as many variables, and we would have no record of which go together. If you use data frames carefully, there is no danger of confusing Dose from the pain relief problem with a Dose vector from some other problem.

Read example two into S-PLUS again, this time calling it pain.relief.

> pain.relief <- read.table("example2", header=T)
> pain.relief
   Dose Water Relief
 1  0.1    10      7
 2  0.2    10     15
 3  0.3    10     19
 4  0.4    10     23
 5  0.1    20     15
 6  0.2    20     13
 7  0.3    20     26
 8  0.4    20     38
 9  0.1    30     21
10  0.2    30     28
11  0.3    30     31
12  0.4    30     47

This data frame consists of 12 observations in three variables. To reference a variable in the pain.relief data frame, put the name of a variable after the name of the data frame, separated by a $ sign.

> pain.relief$Dose
 [1] 0.1 0.2 0.3 0.4 0.1 0.2 0.3 0.4 0.1 0.2 0.3 0.4
> pain.relief$Water
 [1] 10 10 10 10 20 20 20 20 30 30 30 30
> pain.relief$Relief
 [1]  7 15 19 23 15 13 26 38 21 28 31 47
So to do a summary of the variable Relief, the command is:

> summary(pain.relief$Relief)
 Min. 1st Qu. Median  Mean 3rd Qu. Max.
    7      15     22 23.58   28.75   47



Brian Junker 2002-08-26