Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the data type by which R indexes arrays?

Suppose I have some array, but the dimension is a priori unknown (not necessarily 3, as in the example below).

A=array(1:24, dim=c(2,3,4))

I also have an input vector with length equal to the dimension of this array

pos=c(2,1,3)

Based on this input vector, I want to return

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

A[1:2,1:1,1:3]

How can I do this automatically? In other words, what kind of data X do I have to pass to A[] so that R understands what I want.

For example, having X be a list does not work:

A[lapply(pos,function(x) 1:x)]

>Solution :

pos = lapply(pos, seq)
do.call(`[`, c(list(A), pos))

     [,1] [,2] [,3]
[1,]    1    7   13
[2,]    2    8   14
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading