1

General Related Questions

Could someone please point me to functions / methods to get the dimension sizes of a multidimension list or datastructure in R?

It would also be useful to know ways to access individual elements within this bigger datastructure?

Questions on Sample Input and Output Data Below

The Input data is converted to output data using this command:(related question below):

lst <- lapply(split(df2[-1], df2$Column_Zero), function(x) 
         acast(x, Column_Two~Column_One,value.var="Column_Three"))
  1. How many matrices are created?
  2. How to access the name of each sub matrix that is created?
  3. How many columns each sub matrix has?
  4. How many rows each sub matrix has?
  5. Which sub matrix has the maximum / minimum number of columns / rows?

Input Data:

Column_Zero, Column_One, Column_Two, Column_Three

XX,A, 1, 4
XX,A, 2, 3
XX,A, 3, 77
XX,B, 1, 44
XX,B, 2, 32
XX,B, 3, 770
XX,C, 1, 43
XX,C, 2, 310
XX,C, 3, 68       
YY,A1, 1, 4
YY,A1, 2, 3
YY,A1, 3, 77
YY,B1, 1, 44
YY,B1, 2, 32
YY,B1, 3, 770
YY,C1, 1, 43
YY,C1, 2, 310
YY,C1, 3, 68 
YY,D2, 1, 4
YY,D2, 2, 5
YY,D2, 3, 6 

--------- And so on -----

Output Data:

------ Data Table one ------

A, B, C
4, 44, 43
3, 32, 310
77, 770, 68

------ Data Table Two ------

A1, B1, C1, D2
4, 44, 43,4
3, 32, 310,5
77, 770, 68,6

------ and so on -----

Related Question: This comes up in the related question, where we are splitting a data table and reshaping it. So it becomes important to know how big the smaller data structures / matrices or data tables are.

R convert Datatable distinct column values to column names and column values as values from another column

Please let me know if anything is not clear or if you need any further information.

Community
  • 1
  • 1
texmex
  • 151
  • 9
  • Could you show an example of input and desired output for your "related question"? – Rick Jan 26 '16 at 05:02
  • Please read the info on [Ask] and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610) – Jaap Jan 26 '16 at 07:07
  • [See the help-page on how to format your question text](http://stackoverflow.com/editing-help) for more info – Jaap Jan 26 '16 at 08:34

2 Answers2

0
# Is this what you need to see?
k <- 1:60
# data structure with three dimensions
dim(k) <- c(3,4,5)
# Question 1: see the dimensions of k
dim(k)
# Question 2: show a two dimensional part of k
k[,,1]
Rick
  • 888
  • 8
  • 10
  • Thanks for your answer. Please see the related question for sample input and output ... The answer by akrun shows the variables used here. When I tried dim on the output data structure (lst) after the split and the acast, I get Null ... I am able to do this and dim(lst[[1]]) etc. – texmex Jan 26 '16 at 06:04
0

The answers for the questions are

  1. How many matrices are created?

As each of the list element is a matrix, we can use length to find the number of matrices

length(lst)
#[1] 2

In case if there are other objects (in a general scenario)

sum(sapply(lst, is.matrix))
#[1] 2
  1. How to access the name of each sub matrix that is created?

We can use names

names(lst)
  1. How many columns each sub matrix has?

We can find out by looping over the lst

sapply(lst, ncol)
#   XX YY 
#   3  4 
  1. How many rows each sub matrix has?

It is the same as above by replacing ncol with nrow. We can get both by using dim

lapply(lst, dim)
  1. Which sub matrix has the maximum / minimum number of columns / rows?

We can use which.min or which.max to find the index after looping through the 'lst'.

lst[which.min(sapply(lst, ncol) )]
lst[which.max(sapply(lst, ncol))]

and the same can be done for the nrow

akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    … Thanks for your answers. I will try them out shortly. But I have accepted the answer since I know from before that your solutions are spot on. – texmex Jan 26 '16 at 12:58