[R] "[[" gotcha

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Jan 16 11:44:26 CET 2007


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



More information about the R-help mailing list