1
$\begingroup$

OK, let's say we have a following list of lists of lists:

{{{4, 2}, {6, 2}, {2, 2}}, {{6, 3}, {4, 1}, {3, 1}, {2, 1}},      
{{6,2}, {3, 2}, {4, 1}, {1, 1}}, {{4, 2}, {1, 2}, {6, 1}, {3, 1}}, 
{{4,3}, {6, 1}, {1, 1}, {2, 1}}, {{5, 3}, {1, 1}, {2, 1}, {3,1}}, 
{{2, 2}, {5, 2}, {6, 1}, {3, 1}}, {{1, 2}, {5, 2}, {3, 2}}, 
{{6, 2}, {3, 2}, {2, 1}, {5, 1}}, {{3, 3}, {6, 1}, {1, 1}, {5,1}}, 
{{5, 2}, {2, 2}, {4, 1}, {1, 1}}, {{2, 3}, {4, 1}, {6, 1}, {5, 1}}, 
{{1, 3}, {4, 1}, {3, 1}, {5, 1}}, {{4, 2}, {1, 2}, {2, 1}, {5, 1}}}

Now from such list, call it L for convenience, I'd like to create a data frame where there are Length[L] rows (so 14 rows in this case; that number is known in advance btw). Now, columns. I would like to have Length[Range[min,max,1]] columns labeled by Range[min,max,1] where

min = Min[Map[Min,Map[Min,L,{2}],{1}]]

and

max = Max[Map[Max,Map[Max,L,{2}],{1}]]

so for the case above we'd have min=1 and max=6 and so we'd have 6 columns, labelled by 1,2,3,4,5 and 6. I hope that's clear. Now the entries of the data frame. So if we look at the first list of L: {{4,2},{6,2},{2,2}}.That corresponds to the first row of the data frame. Now {{4,2},{6,2},{2,2}}tells us that we would 2 in the 4th column, 2 in the 6th column, 2 in the 2nd column. Also, we would like to have 0's elsewhere, so we'd have 0's in the 1st,3rd and 5th column of the first row. The main objective is to then transfer it to R and do the analysis there. I'd really appreciate some help.

$\endgroup$
2
$\begingroup$

You can use SparseArray as follows:

sa = SparseArray[Join @@ MapIndexed[{#2[[1]], #[[1]]} -> #[[2]] &, L, {2}]];

TeXForm@MatrixForm[sa]

$\left( \begin{array}{cccccc} 0 & 2 & 0 & 2 & 0 & 2 \\ 0 & 1 & 1 & 1 & 0 & 3 \\ 1 & 0 & 2 & 1 & 0 & 2 \\ 2 & 0 & 1 & 2 & 0 & 1 \\ 1 & 1 & 0 & 3 & 0 & 1 \\ 1 & 1 & 1 & 0 & 3 & 0 \\ 0 & 2 & 1 & 0 & 2 & 1 \\ 2 & 0 & 2 & 0 & 2 & 0 \\ 0 & 1 & 2 & 0 & 1 & 2 \\ 1 & 0 & 3 & 0 & 1 & 1 \\ 1 & 2 & 0 & 1 & 2 & 0 \\ 0 & 3 & 0 & 1 & 1 & 1 \\ 3 & 0 & 1 & 1 & 1 & 0 \\ 2 & 1 & 0 & 2 & 1 & 0 \\ \end{array} \right)$

TableForm[sa, TableHeadings -> {Range[Length@sa], Range[Length[sa[[1]]]]}]

enter image description here

$\endgroup$
  • $\begingroup$ This is great, thank you so much @kglr. I can't really figure out how does your sa function or perhaps MapIndexed part of it , "creates" the correct row number? $\endgroup$ – amator2357 Jan 23 at 12:38
  • $\begingroup$ @amator2357, In MapIndexed the slot #2 is a pair that refers to {rowindex, columnindex} and its first part (#2[[1]]) is the row index. $\endgroup$ – kglr Jan 23 at 12:49
  • $\begingroup$ Thank you @kglr. I understood that, I wasn't sure how was the #2[[1]] spitting out the correct row index, but I see it now. Thanks again for your help, much appreciated. $\endgroup$ – amator2357 Jan 23 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.