[R] $

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Jun 20 19:32:38 CEST 2006


On Tue, 2006-06-20 at 12:03 -0500, Davis, Jacob B. wrote:
> Thanks for humbling yourself to my level and not answering my question.
> 
> If object is user defined is:
>  object$df.residual 
>  the same thing as
>  df.residual(object)

Hi,

In this case, yes they are the same, but don't think that if you had
object$foo that you could do foo(object) and get anything useful. Most
of the time you can't and R will throw an error.

df.residual() is an extractor function and these are generally preferred
over extracting the component of the list directly using the $ notation.
Other extractors include coef(), residuals(), fitted() etc.

"$" _is_ used to extract components from lists, and can be used on data
frames as these are just a special type of list.

Look at this example:

y <- runif(100)
x <- rnorm(100)
mod <- lm(y ~ x)
mod
mod$df.residual
df.residual(mod)

If we look at the structure of the lm object, we see many components:

str(mod)

There is a model component, which you could extract using

mod$model

but if you try

model(mod)

you'll get an error about not being able to find function "model".

Hopefully that explains things a little.

Gav



More information about the R-help mailing list