Re: [R] "[[" gotcha

From: Barry Rowlingson <b.rowlingson_at_lancaster.ac.uk>
Date: Tue 16 Jan 2007 - 10:44:26 GMT

Robin Hankin wrote:

> The error is given because after B[[1]] <- a, the variable B is
> just a scalar and
> not a matrix (why is this?)
>

  Because [[i]] indexes more general vectors, and if you do B[[1]] when B is NULL, R doesnt know if you want B to be a list or a simple vector.

  If you initialise B as an empty list then R knows:

  > B=list()
  > B
  list()
  > B[[1]]=b
  > B
  [[1]]

       [,1]
  [1,] 1

Barry



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 and provide commented, minimal, self-contained, reproducible code. Received on Tue Jan 16 21:59:18 2007

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 Tue 16 Jan 2007 - 11:30:28 GMT.

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