[R-sig-eco] Community distance matrix deconstruction

Kate Boersma kateboersma at gmail.com
Sat Dec 13 01:17:34 CET 2014


Hi all.

I have a community analysis data manipulation puzzle for you... hopefully someone can help. Please let me know if this question needs clarification, has previously been answered, or would be better sent to a different list.

Details follow.

Thank you,

Kate

---

Here is a simplified version of my problem:

I ran a community manipulation experiment with 7 reps of 2 treatments, for a total of 14 communities. Communities 1-7 are in Treatment 1 and 8-14 are in Treatment 2. I identified 5 taxa in the 14 communities and calculated a community dissimilarity matrix (14*14). Now I would like to decompose the distance matrix into a dataframe with the following column headings: community1s, community2s, withinORbetweenTRT, and distance. “Within or between treatment” indicates if the distance is between two communities within the same treatment or between the two treatments (values of 0 or 1).

I did it by hand below to demonstrate, but my actual dataset has 100 communities so I need to figure out how to automate it...

df<-data.frame(cbind(1:14, 18:5, 3:16, 14:1, 16:3)) #random values

dist<-dist(df)

distance<-as.vector(dist)

community1s<-c(1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,

4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,

8,8,8,8,8,8,9,9,9,9,9,10,10,10,10,11,11,11,12,12,13)

community2s<-c(2,3,4,5,6,7,8,9,10,11,12,13,14,3,4,5,6,7,8,9,10,11,12,13,14,

4,5,6,7,8,9,10,11,12,13,14,5,6,7,8,9,10,11,12,13,14,

6,7,8,9,10,11,12,13,14,7,8,9,10,11,12,13,14,

8,9,10,11,12,13,14,9,10,11,12,13,14,10,11,12,13,14,

11,12,13,14,12,13,14,13,14,14)

#now I need a column for whether or not the comparison is within treatment or

#between treatments. I ordered the sites by treatment so sites 1-7 are in treatment1

#and 8-14 are in treatment2. 0 is within and 1 is between.

withinORbetweenTRT<-c(0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,

1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,

1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,

0,0)

#now I can assemble the dataframe:

final.df<-cbind(community1s, community2s, withinORbetweenTRT, distance)

final.df

I would appreciate any ideas!

-- 
Kate Boersma, PhD
Department of Biology
University of San Diego
5998 Alcala Park
San Diego CA 92110
kateboersma at gmail.com
http://www.oregonstate.edu/~boersmak/

Kate S. Boersma, Ph.D.
kateboersma at gmail.com
http://people.oregonstate.edu/~boersmak/

Department of Biology
University of San Diego
5998 Alcala Park
San Diego, CA 92110


	[[alternative HTML version deleted]]



More information about the R-sig-ecology mailing list