[R] dimensions dropped on assignment

Ernest Adrogué nfdisco at gmail.com
Tue Jan 31 21:38:59 CET 2012


Hi,

Thanks for your comments, the issue remains unsolved though. I insist
the dropping of dimensions only appears to occur in assignments

> out <- array(0, 5, list(1:5))
> out
1 2 3 4 5 
0 0 0 0 0 
> dim(out)
[1] 5
> dim(out[as.character(2:3)])
[1] 2

There is no dropping here, but then

> out[as.character(2:3)] <- NA
> out
 1  2  3  4  5 
 0 NA NA  0  0 
> dim(out)
NULL

Also, this only happens when labels are used as indices, NOT with
integer indices:

> out <- array(0, 5, list(1:5))
> out[2:3] <- NA
> out
 1  2  3  4  5 
 0 NA NA  0  0 
> dim(out)
[1] 5

I find this very strange. It's not that commas are missing, or the
drop parameter is needed (doesn't work, as shown in my previous e-mail).


31-01-2012, 13:23 (-0500); Richard M. Heiberger escriu:
> > tmp <- matrix(1:12, 3, 4, dimnames=list(letters[1:3], LETTERS[1:4]))
> > tmp
>   A B C  D
> a 1 4 7 10
> b 2 5 8 11
> c 3 6 9 12
> > tmp["a",, drop=FALSE]
>   A B C  D
> a 1 4 7 10
> > tmp[,"A",drop=FALSE]
>   A
> a 1
> b 2
> c 3
> >
> 
> you need the correct number of commas.
> 
> For your example
> 
> > dim(out[1, drop=FALSE])
> [1] 1
> > out <- array(0, 5, list(1:5))
> > dim(out)
> [1] 5
> > dim(out[1])
> NULL
> > dim(out[1, drop=FALSE])
> [1] 1
> >
> 
> This construct usually isn't needed for assignment
> 
> > out[2] <- 12
> > out
>  1  2  3  4  5
>  0 12  0  0  0
> > dim(out)
> [1] 5
> > dim(out[2])
> NULL
> > dim(out[2, drop=FALSE])
> [1] 1
> >
> 
> 
> 2012/1/31 Ernest Adrogué <nfdisco at gmail.com>
> 
> > 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
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >



More information about the R-help mailing list