[R] matrix from sequences

Miha STAUT mihastaut at hotmail.com
Fri Feb 14 22:50:03 CET 2003


>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

>
>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.
>
>Or am I missing something?

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




More information about the R-help mailing list