[R] export/import matrix
Rosario Garcia Gil
M.Rosario.Garcia at slu.se
Mon Aug 1 01:54:17 CEST 2011
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
More information about the R-help
mailing list