Thanks! that worked perfectly

--
Kendra Maas Mitchell, Ph.D.
Post Doctoral Research Fellow
University of British Columbia
604-822-5646
________________________________
From: Karen Kotschy [karen.kotschy@gmail.com]
Sent: Wednesday, July 31, 2013 12:14 AM
To: r-sig-ecology@r-project.org
Cc: Mitchell, Kendra
Subject: Re: [R-sig-eco] subsetting lower triangle distance matrix based on variable in another object

Hi Kendra

I had a similar problem a while back. As Jari says, you have to do the subsetting while the object is an ordinary matrix, and then convert it back to a "dist" object. To convert a distance matrix to an ordinary matrix, use:

mat <- as.matrix(disobject)

Then create the vector k, containing "TRUE" values for the entries you want to select and "FALSE" values for those you don't. For example, in your case (I think):

k <- bac_env$zone=="TX"

Then subset your matrix as Jari suggested and convert it back to a distance matrix:

submat <- mat[k,k]
as.dist(submat)

HTH
Karen


a <- rownames(nutdisdf) %in% names(wa.comm1st)
nutdis.comm1st <- subset(nutdisdf, a == TRUE)   # keep only rows of spp with abund records in comm1st.
nutdis.comm1st <- nutdis.comm1st[a==TRUE]   # keep only cols which match rows above.

On Wed, Jul 31, 2013 at 7:13 AM, Jari Oksanen <jari.oksanen@oulu.fi<mailto:jari.oksanen@oulu.fi>> wrote:
Kendra,

Subset a matrix an convert to dist. You must subset both rows and columns in the same way, and therefore you must use subsetting index instead of a subset() function. Let k be a vector that is TRUE for selected items; as.dist(yoursymmetricmatrix[k, k]) is what you are looking for.

Cheers, Jari Oksanen
---- alkuperäinen viesti ----
Lähettäjä: Mitchell, Kendra
Lähetetty:  31.07.2013, 02:38
Vastaanottaja: "<r-sig-ecology@r-project.org<mailto:r-sig-ecology@r-project.org>>" <r-sig-ecology@r-project.org<mailto:r-sig-ecology@r-project.org>>
Aihe: [R-sig-eco] subsetting lower triangle distance matrix based on variable in another object

I have a number of dissimilarity matrices built in another program that I'd like to subset then run NMS.

#read in distance matrix
b_bc03 <-data.matrix(read.table("bac_final.an.thetayc.0.03.lt.ave.dist", row.names=1, header=T, fill=TRUE))  # creates a 736x736 double matrix

#read in environmental variables
 bac_env<-read.csv("bac_samples.csv", row.names=1, header=T)  # 736 obs. of 6 variables

#The only way that I've figured out how to subset a lt matrix is to convert to dist object first
bc_dist<-as.dist(b_bc03)  # dist[27048]
tx_dist<-subset(bc_dist, bac_env$zone=="TX") # numeric[54316]

Here is where it falls apart, the subset doesn't keep the labels, just the values so I can't convert it back to a distance matrix.  I've looked through ecodist but can't find anyway in that package to select only certain samples.

Help please?

thanks
Kendra



--
Kendra Maas Mitchell, Ph.D.
Post Doctoral Research Fellow
University of British Columbia
604-822-5646

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-ecology mailing list
R-sig-ecology@r-project.org<mailto:R-sig-ecology@r-project.org>
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology

_______________________________________________
R-sig-ecology mailing list
R-sig-ecology@r-project.org<mailto:R-sig-ecology@r-project.org>
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology


	[[alternative HTML version deleted]]

