[R] How to refer the element in a named list?

Peng Yu pengyu.ut at gmail.com
Sun Sep 6 19:04:51 CEST 2009


Hi,

I thought that 'coefficients' is a named list, but I can not refer to
its element by something like r$coefficients$y. I used str() to check
r. It says the following. Can somebody let me know what it means?
  ..- attr(*, "names")= chr [1:2] "(Intercept)" "y"

$ Rscript lm.R
> x=1:10
> y=1:10
> r=lm(x~y)
> class(r)
[1] "lm"
> mode(r)
[1] "list"
> r

Call:
lm(formula = x ~ y)

Coefficients:
(Intercept)            y
  1.123e-15    1.000e+00

> r$coefficients[1]
 (Intercept)
1.123467e-15
> r$coefficients[[1]]
[1] 1.123467e-15
> r$coefficients[2]
y
1
> r$coefficients[[2]]
[1] 1
>


> str(r)
List of 12
 $ coefficients : Named num [1:2] 1.12e-15 1.00
  ..- attr(*, "names")= chr [1:2] "(Intercept)" "y"

Regards,
Peng




More information about the R-help mailing list