[R] Loss of dimensions in subsetting arrays

Berend Hasselman bhh at xs4all.nl
Fri Oct 12 17:55:15 CEST 2012


On 12-10-2012, at 11:52, "Markku Karhunen" <markku.karhunen at helsinki.fi> wrote:

> 
> Hi all,
> 
> I've been wondering for a long time why R drops the dimensions of an array/matrix when you try to take a subset of one column. I mean this:
> 
> dim(A)
> [1] 2 5 2
> B=A[1,,]

Use 

B <- A[1,,,drop=FALSE]

Also read the help for [:

?"["

Berend

> dim(B)
> 5 2 # so now dim(B)[3] doesn't work
> C=B[2,]
> dim(C)
> NULL # so now nrow(C) doesn't work
> 
> Typically, you can get rid of this by writing as.matrix, as.array(...) but that generates extra lines of code. This is really annoying. Does anybody know how to turn this behaviour off?
> 
> best,
> Markku Karhunen
> Uni. Helsinki
> 
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list