[R] [] vs [[]]

Bill Simpson wsi at gcal.ac.uk
Fri Oct 20 10:40:58 CEST 2000


I have read over Venables and Ripley and am still confused about [] vs
[[]]. I had thought it was simple: use [] for accessing items in a vector,
and [[]] for items in a list.

Vector
> x<-1:10
> x[4]
[1] 4
> x[[4]]
[1] 4
doesn't seem to matter if you use [] or [[]]

List
> x<-list(1,2,3,4)
> x[2]
[[1]]
[1] 2

> x[[2]]
[1] 2
doesn't seem to matter if you use [] or [[]]

This question started from nls(). If you do
fit<-nls(...)
using a 2 param model
> coef(fit)
          a           b 
212.6836297   0.0641211 
> coef(fit)[1]
       a 
212.6836 
> coef(fit)[[1]]
[1] 212.6836

I guess coef(fit) returns some weird sort of list and so I need
coef(fit)[[]] to get the fitted parameters.

As you can see I am confused so any help appreciated.

Bill

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list