1998-02-06 (Week 5)

Some Useful Details...

The safest way to remove a variable from a data frame is to copy the columns you want to keep to a new data frame. Don't over-write the original data frame in case you mess up and need it again.

For example, operc has 11 variables; the 11th is lingrow1. To make a new data frame called operc2 without lingrow1, you could use

operc2<-operc[,1:10]

The outlier in lingrow is less than -4, as we saw on the graph. A single statement to replace this outlier with NA in a new column called lingrow1 is

operc$lingrow1 <- ifelse(operc$lingrow<(-4),NA,operc$lingrow)

What would happen if I omitted the parentheses around -4? (Warning: that error would make a mess!)

To use inage as a factor in the anova, I declared

operc$inage <- as.factor(operc$inage)

If I later wanted to make it numeric again, I could use

operc$inage <- as.numeric(as.character(operc$inage))

if I wanted the original numbers back. If I used

operc$inage <- as.numeric(operc$inage)

I would just get the factor levels as integers and the original values (which had been kept as character strings naming the levels of the factor) would be lost.

On some installations (hydra, e.g.), read.table will automatically make any character or categorical variable a factor unless as.is=T is given in the argument list.

You will avoid difficulties if you understand the different modes of S objects (character, numeric, function, etc.)

Here is how to determine the length of a vector, and two ways to determine the number of non-NA values.

> length(ingrow)
[1] 1953
> length(ingrow[!is.na(ingrow)])
[1] 1638
> length(ingrow) - length(which.na(ingrow))
[1] 1638
 


What to Prepare for the Next Class...

Work on Assignment #1. You may work in groups of up to three students if you wish.

Import the "Habitat availability" data into S and start exploring it. What kind of model could you suggest for these data?

Read the Preface, the Prelude, "How to tackle statistical problems," and Chapters 1 to 6 of Chatfield, Problem Solving: A Statistician's Guide. Be prepared to discuss the ideas in class. You will find this material very useful as you work on the Assignment.


Back to S4P03