[R] manipulate a matrix
Marc Schwartz
marc_schwartz at comcast.net
Mon Jun 25 19:22:29 CEST 2007
On Mon, 2007-06-25 at 10:58 -0400, Jon Hak wrote:
> I have read everything I can find on how to manipulate a results
> matrix in R and I have to admit I'm stumped. I have set up a process
> to extract a dataset from ArcGIS to compute a similarity index
> (Jaccards) in Vegan. The dataset is fairly simple, but large, and
> consists of rows = sample area, and columns = elements. I've been
> able to view the results in R, but I want to get the results out to a
> database and a matrix that is 6000-rows x 6000-columns can be very
> difficult to manipulate in Windows XP. I would to rotate the matrix
> so that the output would look like the old condensed format in
> programs like Conoco. Ideally, I would like format to look something
> like this;
>
>
> Site-row Site-col Jaccard
> 1 1 1
> 1 2 .9
> 1 3 .6
> 2 1 .9
> 2 2 1
> 2 3 .75
>
> Thanks for any help,
Presuming that your source matrix for the above is:
mat <- matrix(c(1, .9, .6, .9, 1, .75), ncol = 3, byrow = TRUE)
> mat
[,1] [,2] [,3]
[1,] 1.0 0.9 0.60
[2,] 0.9 1.0 0.75
You can use the following:
t.mat <- t(mat)
Res <- cbind("Site-row" = as.vector(col(t.mat)),
"Site-col" = as.vector(row(t.mat)),
Jaccard = as.vector(t.mat))
> Res
Site-row Site-col Jaccard
[1,] 1 1 1.00
[2,] 1 2 0.90
[3,] 1 3 0.60
[4,] 2 1 0.90
[5,] 2 2 1.00
[6,] 2 3 0.75
See ?t and ?row
HTH,
Marc Schwartz
More information about the R-help
mailing list