Loss of dimensions in subsetting arrays

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Loss of dimensions in subsetting arrays

Markku Karhunen-2

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,,]
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loss of dimensions in subsetting arrays

Marc Schwartz-3
On Oct 12, 2012, at 4:52 AM, Markku Karhunen <[hidden email]> 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,,]
> 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


You can save yourself a lot of time if you visit the R FAQ as your first action item when such questions come up. In this case:

  http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-my-matrices-lose-dimensions_003f

Regards,

Marc Schwartz

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loss of dimensions in subsetting arrays

Berend Hasselman
In reply to this post by Markku Karhunen-2

On 12-10-2012, at 11:52, "Markku Karhunen" <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loss of dimensions in subsetting arrays

David Winsemius
In reply to this post by Markku Karhunen-2

On Oct 12, 2012, at 2:52 AM, Markku Karhunen 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,,]
> 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?

Read the help page for:

 ?"["  # especially regarding the drop parameter.

--

David Winsemius, MD
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loss of dimensions in subsetting arrays

Markku Karhunen-2

Ok, apologies.

>
> On Oct 12, 2012, at 2:52 AM, Markku Karhunen 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,,]
>> 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?
>
> Read the help page for:
>
>  ?"["  # especially regarding the drop parameter.
>
> --
>
> David Winsemius, MD
> Alameda, CA, USA
>
>

______________________________________________
[hidden email] 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.