[R] dimensions dropped on assignment

Ernest Adrogué nfdisco at gmail.com
Tue Jan 31 17:04:01 CET 2012


Hi there,

This is a problem I've run into and do not know how to avoid. It
happens when I make an assignment using the dimension names as the
subscript of the array. The end result is a dimenensionless array
(i.e. a vector) which I don't want. See:

> out <- array(0, 5, list(1:5))
> dim(out)
[1] 5
> out[names(out)] <- 1
> dim(out)
NULL

I tried to include a 'drop' argument into the index but it doesn't
seem to work:

> out[names(out), drop=FALSE] <- 1
Error in out[names(out), drop = FALSE] <- 1 : 
  incorrect number of subscripts on matrix

A solution would be to change the vector into an array myself, but I
would like to hear opinions before I do that, because I would rather
just make the assignment and keep the array object unchanged if such
thing is possible. Also, using a subscript of integers instead of
labels is not an option in this case.

Any help appreciated.

-- 
Cheers,
Ernest



More information about the R-help mailing list