Skip to content

export/import matrix

4 messages · Rosario Garcia Gil, Dieter Menne, David Winsemius +1 more

#
Hello

I have a problem on keeping the format when I export a matrix file with the write.table() function.

When I import the data volcano from rgl package it looks like this in R:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]  100  100  101  101  101  101  101  100  100   100   101   101   102   102
[2,]  101  101  102  102  102  102  102  101  101   101   102   102   103   103
[3,]  102  102  103  103  103  103  103  102  102   102   103   103   104   104
[4,]  103  103  104  104  104  104  104  103  103   103   103   104   104   104
[5,]  104  104  105  105  105  105  105  104  104   103   104   104   105   105

I use this data to represent a 3D map with the follwing script and it works PEFECT!
Then I export it as write.table(data, file="datam.txt", row.names=TRUE, col.names=TRUE),

when I import it back into R again with read.table("datam.txt") it looks like this in R:


   V1  V2  V3  V4  V5  V6  V7  V8  V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19
1 100 100 101 101 101 101 101 100 100 100 101 101 102 102 102 102 103 104 103
2 101 101 102 102 102 102 102 101 101 101 102 102 103 103 103 103 104 105 104
3 102 102 103 103 103 103 103 102 102 102 103 103 104 104 104 104 105 106 105
4 103 103 104 104 104 104 104 103 103 103 103 104 104 104 105 105 106 107 106
5 104 104 105 105 105 105 105 104 104 103 104 104 105 105 105 106 107 108 108

The script I mention before does not anymore work on it, if I converted to matrix with as.matrix still does not work.

I have read the pdf on import/export of R and searched by googleling but I have not found any answer to my problem.

I am sorry if the answer is very obvious but I have tried for more than a week.

Any help is really wellcome, thanks in advance.
Rosario
#
Rosario Garcia Gil-2 wrote:
It is always better to report what str(mydata) looks like, instead of
showing the data. And I an quite sure that something like as.matrix would
work, but you did not tell use what the error message in "still does not
work" looked like.

Dieter




--
View this message in context: http://r.789695.n4.nabble.com/export-import-matrix-tp3708935p3709072.html
Sent from the R help mailing list archive at Nabble.com.
#
On Jul 31, 2011, at 7:54 PM, Rosario Garcia Gil wrote:

            
The quick answer is ... don't do that. Use save() if you want to  
preserve the attributes of an R object. And that especially applies if  
you don't understand the differences between R object types.

I have discarded a longer answer that complained about your failure to  
provide complete code.
#
If you are just exporting it so you can read it back into R later, it
is better to use save/load since it keep the data in the internal
format so it will look the same.

Can you describe what you are going to be doing with the data that you
'export'; that might help us come up with a solution to your problem.

On Sun, Jul 31, 2011 at 7:54 PM, Rosario Garcia Gil
<M.Rosario.Garcia at slu.se> wrote: