Re: [R] $

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Wed 21 Jun 2006 - 03:32:38 EST

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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jun 21 03:44:07 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 21 Jun 2006 - 04:12:01 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.