[R] Converting matrices into row vectors and saving as ASCII text

David Winsemius dwinsemius at comcast.net
Sun Sep 20 03:16:43 CEST 2009



?cat
?apply
?t

You could follow each row of the transposed matrix with a <newline>:

apply(t(XYbyT), 1, function(x) cat(x, "\n", file="output.txt",  
append=TRUE) )


On Sep 19, 2009, at 8:11 PM, Xi Ang wrote:

>
>
> Thanks for your reply.
>
> Is there a way I can save the data to an ascii file without losing the
> row/column structure?
> I have tried save(...) and write.table(...) but the output file  
> seems to
> jumble up the order of the matrix.
>
> Thanks
> Xi
>
>
> David Winsemius wrote:
>>
>> XYT <- array(1:150, dim=c(3,5,10))
>> XYbyT= matrix(apply(XYT, 3, I), ncol=10)
>>
>> ...or even...
>>
>> XYbyT= matrix(XYT, ncol=10)
>>
>> --  
>> David.
>>
>> On Sep 19, 2009, at 1:11 PM, Xi Ang wrote:
>>
>>>
>>> Hi
>>>
>>> I have some data with these dimensions:
>>> 5 3 100
>>>
>>> which correspond to the x, y, and time dimensions, for a variable,  
>>> p.
>>>
>>> I need the data in this format: 100 rows (1 row per time unit),  
>>> and 15
>>> values in each row.
>>>
>>> I have attempted to reshape my data
>>>
>>>> dim(data)
>>> 5 3 100
>>>
>>>> attr(data,'dim')<-c(dim(data)[3],dim(data)[1]*dimdata[2])
>>>
>>> So I get data with 100 rows, 15 columns.
>>>
>>> I need to use this data outside of R, and so have to save it as an
>>> ASCII
>>> file that retains the row-column structure of the data, but I do not
>>> know
>>> how to.
>>>
>>> It would be ideal if I could end up with a text file that also has  
>>> an
>>> additional column that labels which time unit (1-100) the row
>>> belongs to,
>>> i.e.
>>>
>>> 1   a1,1  a1,2 ........... a1,15
>>> 2   a2,1  a2,2 ........... a2,15
>>> 3   a3,1  a3,2 ........... a3,15
>>> 4   a4,1  a4,2 ........... a4,15
>>> .
>>> .
>>> .
>>> 99
>>> 100
>>>
>>> Any suggestions would be appreciated.
>>>
>>> Thanks
>>> Xi
>>>
>>>
>>> -- 
>>> View this message in context:
>>> http://www.nabble.com/Converting-matrices-into-row-vectors-and-saving-as-ASCII-text-tp25523562p25523562.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at r-project.org 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.
>>
>> David Winsemius, MD
>> Heritage Laboratories
>> West Hartford, CT
>>
>> ______________________________________________
>> R-help at r-project.org 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.
>>
>>
>
> -- 
> View this message in context: http://www.nabble.com/Converting-matrices-into-row-vectors-and-saving-as-ASCII-text-tp25523562p25526729.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list