[R] export/import matrix
David Winsemius
dwinsemius at comcast.net
Mon Aug 1 14:42:42 CEST 2011
On Jul 31, 2011, at 7:54 PM, Rosario Garcia Gil wrote:
> Hello
>
> I have a problem on keeping the format when I export a matrix file
> with the write.table() function.
>
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.
--
David
> When I import the data volcano from rgl package it looks like this
> in R:
>
>
>> data[1:5,]
> [,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!
>
>> y<- 2*data
>> x <- 10* (1:nrow(y))
>> z <- 10* (1:ncol(y))
>> ylim <- range(y)
>> ylen <-ylim[2] - ylim[1] + 1
>> colorlut <- terrain.colors(ylen)
>> col <- colorlut[y-ylim[1] + 1]
>> rgl.open()
>> rgl.surface(x,z,y, color=col, back="lines")
>
>
> 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
> ______________________________________________
> 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
West Hartford, CT
More information about the R-help
mailing list