[R] matrix from sequences

Roger Bivand Roger.Bivand at nhh.no
Sat Feb 15 10:54:03 CET 2003


On Fri, 14 Feb 2003, Miha STAUT wrote:

> >Hi, Miha:
> >
> >1.  How do I get the GRASS library?  "library(GRASS)" produced "Error in 
> >library(GRASS) : There is no package called `GRASS'" for me from R 1.6.2 
> >for Windows.
> 
> I do not know whether it exists for Windows or not, but look under:
> http://cran.r-project.org/src/contrib/Devel/
> Or visit:
> http://grass.itc.it/index.html

There is as yet no pre-compiled GRASS package for GRASS/Cygwin and 
R/MinGW, although there are hints on the GRASS site about how one might 
build one from the GRASS source package (look for the geostatistics link). 

(Incidentally, library(GRASS) loads an installed package - you would need 
to install it first for library() to find it)

This has been the subject of discussion on the GRASS developers' list 
recently, and a revised source package will be released soon that will 
also build using the standard R MinGW toolset under Windows. 

> 
> >
> >2.  I assume there is a typographical error in the last line of your email: 
> >  If G$xseq and $yseq are coordinates of points, then length(G$xseq) == 
> >length(G$yseq)???  In that case, 'as.matrix(G[,c("xseq", "yseq")])' should 
> >give you what you want.


length(G$xseq) == length(G$yseq) only by coincidence for square grids. The 
values of xseq are the midpoint eastings of raster columns, of yseq the 
midpoint northings (north to south) of the raster rows, and are mostly 
used to get image() to display data correctly. There are two helper 
functions: east(G) and north(G), that unroll the sequences to give a 
complete list of raster cell midpoints in the order GRASS rasters store 
data (starting at the NW (aka top left) corner). So:

cbind(east(G), north(G))

should give you what you need. You will see this used inside krige.G() in 
the same package (there optionally checking for a mask too).

Please contact me off-list, or via the dedicated R/GRASS list described 
on: http://grass.itc.it/statsgrass/index.html

Roger

> OK I really am lousy at explaining things. The length(G$xseq) * 
> length(G$yseq) stands because you have to get all the permutations of the 
> elements of those two sequences. Get it? If you have:
> xseq<-1:10
> yseq<-1:10
> I would like to get:
>       x
> y     [,1] [,2] [,3] [,4] [,5] ...
> [1,]
> [2,]
> [3,]
> [4,]
> ...
> 
> or
> 
> str(xy)
> $x 1,1,1,1,1,1,1,1,1,1,2,2,2,...
> $y 1,2,3,4,5,6,7,8,9,10,1,2,3,...
> 




> 
> >Spencer Graves
> >
> >Miha STAUT wrote:
> >>>Miha STAUT wrote:
> >>>
> >>>>Hi all,
> >>>>
> >>>>I have a data frame with sequences of x and y from a map. I would like 
> >>>>to know it both ways:
> >>>>1. How to make a matrix from that;
> >>>>2. how to make a data frame of all points in a map.
> >>>>
> >>>>Probably it is a silly question, but please tell me where to read about 
> >>>>it or tell me how to do it.
> >>>>
> >>>>Miha Staut
> >>>>
> >>>>______________________________________________
> >>>>R-help at stat.math.ethz.ch mailing list
> >>>>http://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >>>>
> >>>Hi Miha,
> >>>
> >>>1) What is the structure of your data.frame ? Assuming all co-ordinates 
> >>>are in the same column (one column for x and one column for y), the 
> >>>simplest way to extract them and turn them into a matrix would be:
> >>>
> >>>as.matrix(mydata[ , c("x", "y")])
> >>>
> >>>e.g.:
> >>>
> >>>R>mydata <- data.frame(x = rnorm(10), y = rnorm(10), z = rnorm(10))
> >>>R>mydata
> >>>              x           y          z
> >>>1  -0.73735224 -0.51218243 -0.9602624
> >>>2  -1.46079091 -0.63634091  1.4967066
> >>>3  -0.28574919 -1.30719383 -0.2887403
> >>>4   0.04137159  0.61711350 -0.7057102
> >>>5   0.03179303  0.05734869 -0.4637660
> >>>6  -0.06638058 -0.74565157  0.9239402
> >>>7  -0.67611541 -1.01760810 -0.2854017
> >>>8   0.34215052  0.30564550  0.6931193
> >>>9   0.83597837  0.75443762 -2.3394679
> >>>10 -0.14967073 -0.02027512 -0.1143414
> >>>R>as.matrix(mydata[ , c("x", "y")])
> >>>              x           y
> >>>1  -0.73735224 -0.51218243
> >>>2  -1.46079091 -0.63634091
> >>>3  -0.28574919 -1.30719383
> >>>4   0.04137159  0.61711350
> >>>5   0.03179303  0.05734869
> >>>6  -0.06638058 -0.74565157
> >>>7  -0.67611541 -1.01760810
> >>>8   0.34215052  0.30564550
> >>>9   0.83597837  0.75443762
> >>>10 -0.14967073 -0.02027512
> >>>
> >>>
> >>>2) How are the points stored ? If in a matrix, say mat, with 2 columns 
> >>>for x and y, simply:
> >>>
> >>>as.data.frame(mat)
> >>>
> >>>Best,
> >>>
> >>>Renaud
> >>
> >>
> >>
> >>Thanks to both of you (Dr Renaud Lancelot and James Holtman)
> >>
> >>I see I formulated the question in a wrong way. I got from GRASS the 
> >>coordinates of a map. There is a package in R named GRASS to connect R 
> >>with GRASS.
> >>
> >>library(GRASS)
> >>G<-gmeta() # copy the environment from GRASS
> >>
> >>Now G is a data frame containig also $xseq and $yseq which would be the 
> >>coordinates of all the points in x and y direction. The final matrix 
> >>should have length(G$xseq) * length(G$yseq) points.
> >>
> >>Miha Staut
> >>
> >>______________________________________________
> >>R-help at stat.math.ethz.ch mailing list
> >>http://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
e-mail: Roger.Bivand at nhh.no




More information about the R-help mailing list