[R] matrix from sequences

Miha STAUT mihastaut at hotmail.com
Fri Feb 14 21:53:03 CET 2003


>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




More information about the R-help mailing list