The following gotcha caught me off-guard just now. I have two matrices, a and b: a <- matrix(1,3,3) b <- matrix(1,1,1) (note that both "a" and "b" are matrices). I want them in a list: > B <- NULL > B[[1]] <- a > B[[2]] <- b > B [[1]] [,1] [,2] [,3] [1,] 1 1 1 [2,] 1 1 1 [3,] 1 1 1 [[2]] [,1] [1,] 1 > This is fine. But swapping "a" and "b" over does not behave as desired: > B <- NULL > B[[1]] <- b > B[[2]] <- a Error in B[[2]] <- a : more elements supplied than there are to replace > The error is given because after B[[1]] <- a, the variable B is just a scalar and not a matrix (why is this?) What's the bulletproof method for assigning matrices to a list (whose length is not known at runtime)? -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
Hi try different way of list definition, see below. On 16 Jan 2007 at 10:10, Robin Hankin wrote: From: Robin Hankin <r.hankin at noc.soton.ac.uk> Date sent: Tue, 16 Jan 2007 10:10:42 +0000 To: RHelp help <r-help at stat.math.ethz.ch> Subject: [R] "[[" gotcha> The following gotcha caught me off-guard just now. > > I have two matrices, a and b: > > > a <- matrix(1,3,3) > b <- matrix(1,1,1) > > (note that both "a" and "b" are matrices). > > I want them in a list: >B<-vector("list", 2) B[[1]]<-a B[[1]]<-b here is no complain. HTH Petr> > B <- NULL > > B[[1]] <- a > > B[[2]] <- b > > B > [[1]] > [,1] [,2] [,3] > [1,] 1 1 1 > [2,] 1 1 1 > [3,] 1 1 1 > > [[2]] > [,1] > [1,] 1 > > > > > This is fine. > > But swapping "a" and "b" over does not behave as desired: > > > > B <- NULL > > B[[1]] <- b > > B[[2]] <- a > Error in B[[2]] <- a : more elements supplied than there are to > replace > > > > > > The error is given because after B[[1]] <- a, the variable B is > just a scalar and not a matrix (why is this?) > > What's the bulletproof method for assigning matrices to a list (whose > length is not known at runtime)? > > > > > > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-help at 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.Petr Pikal petr.pikal at precheza.cz
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
To create a empty list do: B <- list() /H On 1/16/07, Robin Hankin <r.hankin at noc.soton.ac.uk> wrote:> The following gotcha caught me off-guard just now. > > I have two matrices, a and b: > > > a <- matrix(1,3,3) > b <- matrix(1,1,1) > > (note that both "a" and "b" are matrices). > > I want them in a list: > > > B <- NULL > > B[[1]] <- a > > B[[2]] <- b > > B > [[1]] > [,1] [,2] [,3] > [1,] 1 1 1 > [2,] 1 1 1 > [3,] 1 1 1 > > [[2]] > [,1] > [1,] 1 > > > > > This is fine. > > But swapping "a" and "b" over does not behave as desired: > > > > B <- NULL > > B[[1]] <- b > > B[[2]] <- a > Error in B[[2]] <- a : more elements supplied than there are to replace > > > > > > The error is given because after B[[1]] <- a, the variable B is > just a scalar and > not a matrix (why is this?) > > What's the bulletproof method for assigning matrices to a list (whose > length is > not known at runtime)? > > > > > > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-help at 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. >
Robin Hankin wrote:> The following gotcha caught me off-guard just now. > > I have two matrices, a and b: > > > a <- matrix(1,3,3) > b <- matrix(1,1,1) > > (note that both "a" and "b" are matrices). > > I want them in a list: > > > B <- NULL > > B[[1]] <- a > > B[[2]] <- b > > B > [[1]] > [,1] [,2] [,3] > [1,] 1 1 1 > [2,] 1 1 1 > [3,] 1 1 1 > > [[2]] > [,1] > [1,] 1 > > > > > This is fine. > > But swapping "a" and "b" over does not behave as desired: > > > > B <- NULL > > B[[1]] <- b > > B[[2]] <- a > Error in B[[2]] <- a : more elements supplied than there are to replace > > > > > > The error is given because after B[[1]] <- a, the variable B is > just a scalar and > not a matrix (why is this?) > > What's the bulletproof method for assigning matrices to a list (whose > length is > not known at runtime)? > >not sure about "bulletproof", but: you should tell R that B is really intended to be a list in the first place: B <- list() the rest then works as you intended. whether the 'simplification' of your 1x1 matrix to a scalar in your example is canonical (and desirable) behaviour seems a question for some of the experts (it's a bit reminiscent of the `drop = TRUE' vs. `drop = FALSE' problem) joerg> > > > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-help at 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.
On Tue, 16 Jan 2007, Robin Hankin wrote:> The following gotcha caught me off-guard just now.Checking the relevant help page is always a good idea (and that is why the posting guide asks that you do so before posting).> I have two matrices, a and b: > > > a <- matrix(1,3,3) > b <- matrix(1,1,1) > > (note that both "a" and "b" are matrices). > > I want them in a list: > > > B <- NULLSo why did you not create a list, e.g. by list() or vector("list", 2)?> > B[[1]] <- a > > B[[2]] <- b > > B > [[1]] > [,1] [,2] [,3] > [1,] 1 1 1 > [2,] 1 1 1 > [3,] 1 1 1 > > [[2]] > [,1] > [1,] 1 > > > > > This is fine. > > But swapping "a" and "b" over does not behave as desired: > > > > B <- NULL > > B[[1]] <- b > > B[[2]] <- a > Error in B[[2]] <- a : more elements supplied than there are to replace > > > > > > The error is given because after B[[1]] <- a, the variable B is > just a scalar and > not a matrix (why is this?)You said wanted a list, and got a numeric vector (R has no scalars). In your first example you got a length-one list, not a matrix. The type-promotion rules for [<- and [[<- are complex, and you should not rely on knowing what they currently are (they do change from time to time). But this one is right there on the help page (?"[["): When '$<-' is applied to a 'NULL' 'x', it first coerces 'x' to 'list()'. This is what also happens with '[[<-' if the replacement value 'value' is of length greater than one: if 'value' has length 1 or 0, 'x' is first coerced to a zero-length vector of the type of 'value'.> What's the bulletproof method for assigning matrices to a list (whose > length is not known at runtime)?Start with a list. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595