[R] Importing and exporting threedimensional arrays
Bert Gunter
bgunter.4567 at gmail.com
Fri Nov 11 17:30:32 CET 2016
Ferri:
I am not sure that I adequately understand your question, but I'll
give it a shot.
1) Do a Web Search on "long form data"; in particular, the following
seemed to be most helpful, but you may prefer one of the other hits:
http://stanford.edu/~ejdemyr/r-tutorials/wide-and-long/
2) See ?reshape in R for R's take on this.
3) If this is not helpful, note that any 3d array can be
conceptualized as a list of 2d matrices, and this immediately gives an
algorithm to convert a 3-d array to a 2-d matrix which can then be
outputted as an ascii table via write.table() . e.g.
> a <- array(1:24,dim=2:4)
> a
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
, , 3
[,1] [,2] [,3]
[1,] 13 15 17
[2,] 14 16 18
, , 4
[,1] [,2] [,3]
[1,] 19 21 23
[2,] 20 22 24
> b <-do.call(rbind,lapply(1:4,function(i)I(a[,,i])))
> b
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[3,] 7 9 11
[4,] 8 10 12
[5,] 13 15 17
[6,] 14 16 18
[7,] 19 21 23
[8,] 20 22 24
Note, however, that this is quite inefficient, and a much faster way
to do this would be to take advantage of the column major order in
which arrays are stored; i.e. an array is actually a vector with a
"dim" attribute. However, I'm too lazy to fool with that now .
But if I understand correctly, your probably better off using long
form data in R to process your data as a (tabular) data frame and
forget about the 3d business altogether.
HTH
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Fri, Nov 11, 2016 at 4:36 AM, Ferri Leberl <ferri.leberl at gmx.at> wrote:
>
>
> Thanks for your answer.
>
> I'm afraid it doesn't.
>
> The planned workflow should be:
>
> Somebody delivers me a table containing the essential relations of an XML-schema in form of a list that should, once it is functional, be generated out of R. So I need a form that is as universal as possible — therefore I thought of a tabular separated list.
> If I make an array:
>
> feld<-array(1:60,dim=c(4,3,5))
>
> save it
>
> save(file="feld",list="feld")
>
> and read it in the bash
>
> cat feld
>
> I receive
>
> U�G�@
> @Q��z���,9� !��w�?,��3c[�/g�2mSD4ѡ鄆��C��r�>�#�'�]劸���?P5�a��(b�#�$RH#�,rȣ�"J(��*j���&Zh��.z�c�!F▒c�)f�c
> K���[����ϼ�Н��;�o|�����Y��N
>
> So I guess it will be a science of its own to understand, how the file is structured.
>
> So I need either a format that allows to easily parse the import 3D-Matrix outside R, or I need any good idea how to avoid the third dimension (e.g., might it function to use a second separation character within the collumns which may contain several items?
>
> Thank you in advance!
> Have a pleasant weekend.
> Yours, Ferri
>
>
>
>
>
> Gesendet: Freitag, 11. November 2016 um 12:02 Uhr
> Von: "Rui Barradas" <ruipbarradas at sapo.pt>
> An: "Ferri Leberl" <ferri.leberl at gmx.at>, "r-helpr-project.org" <r-help at r-project.org>
> Betreff: Re: [R] Importing and exporting threedimensional arrays
> Hello,
>
> You can save 3D objects (or objects of any form or shape) by using ?save.
> You would then retrieve them with ?load.
>
> Hope this helps,
>
> Rui Barradas
>
> Em 11-11-2016 09:36, Ferri Leberl escreveu:
>>
>> Dear all,
>>
>> I want to process a list of XML-Elements.
>> In one dimension the elements are listed; in the other their respective properties (name, comment, parent, children, attributes).
>>
>> I am writing a script that processes such tables, and another one that produces sample tables to test the first script.
>> So the first script should import tabular-separated lists, and the second should export them with write.table (or anything better you suggest me).
>>
>> As long as we stay two dimensional I see no difficulties.
>>
>> However, there can be several children and several attributes. So my idea was to add a third dimension — but some tests I did showed me that e.g.
>>
>> write.table(array(1:60,dim=c(4,3,5)),"beispielmatrix",sep="\t",quote=FALSE,row.names=F,col.names=F,dec=",")
>>
>> produces a 2D-tsv.
>>
>> So, how can I handle the fact, that there may be several items in a children resp. atrribute field?
>>
>> Thank you in advance!
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html[http://www.R-project.org/posting-guide.html]
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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