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:> 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
Rosario Garcia Gil-2 wrote:> > 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: > >> 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 > ... > 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: > > ... > The script I mention before does not anymore work on it, if I converted to > matrix with as.matrix still does not work. > > ... >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:> 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
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:> 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: > > >> 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?