[R] matrix to coordinates

Thomas W Blackwell tblackw at umich.edu
Thu Apr 24 20:06:26 CEST 2003


The two functions  row(), col()  are useful here:

aa <- data.frame(r=as.vector(row(m)), c=as.vector(col(m)), v=as.vector(m))

There's no other ready-made function for this, AFAIK.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -

On Thu, 24 Apr 2003, juli g. pausas wrote:

> Dear R-users,
> I'm sure it must be a specific function or a better way to convert
> matrix to x,y,z coordinates (and viceversa), than my function below (it
> works). Any help?
>
> m2coord <- function(m)
> {
> k <- nrow(m)*ncol(m)
> aa <- data.frame(r=1:k, c=1:k, v=1:k)
> k <- 0
>   for (i in 1:nrow(m))
>   for (j in 1:ncol(m))
>   {
>   k <- k+1
>   aa$f[k]=i; aa$c[k]=j; aa$v[k]=m[i,j]
>   }
> aa
> }
>
> Juli
> --
> Juli G. Pausas
> Centro de Estudios Ambientales del Mediterraneo (CEAM)
> C/ C.R. Darwin 14, Parc Tecnologic,
> 46980 Paterna, Valencia, SPAIN
> Tel: (+ 34) 96 131 8227; Fax: (+ 34) 96 131 8190
> mailto:juli at ceam.es
> http://www.gva.es/ceam



More information about the R-help mailing list