[R] Script to count unique values from two linked matricies

Rui Barradas ruipbarradas at sapo.pt
Tue Sep 25 11:30:48 CEST 2012


Hello,

Try the following.


countTrait <- function(x) length( unique(x[x != 0]) )

presence <- 1*(!is.na(dat1[-1]))
result <- apply(presence, 2, function(x) apply(dat2[, -1]*x, 2, countTrait))
result <- t(result)
rowSums(result)
#Site1 Site2 Site3
#   5     7     3

Hope this helps,

Rui Barradas
Em 25-09-2012 09:11, Benjamin Gillespie escreveu:
> Hi,
>
> Thanks for helping me with this one.
>
> To save you time, the following is the code for the tables I uploaded as jpegs that you may not have received:
>
> dat1<-data.frame(Species=paste("Species",1:3),Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA))
> dat2<-data.frame(Species=paste("Species",1:3),TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1))
> dat3<-data.frame(Site=paste("Site",1:3),Trait_Richness=c(5,7,3))
>
> So,
>
> I have two matricies, dat1 and dat2.
>
> Dat1 is a species abundance matrix. Dat2 is a species trait matrix.
>
> I want to create dat3 through use of a script.
>
> Dat 3 is a count of unique traits observed at each site. i.e. at site 1, species 1 and 3 are present (ass seen in dat1). Species 1 has traits: 1, 2 and 5 for trait types 1, 2 and 3 respectively. Species 3 has traits: 3, 2 and 1 for trait types 1, 2 and 3 respectively.
>
> So, at site 1:
>
> For trait type 1, 2 unique traits were observed. For trait type 2, 1 unique trait was observed (both species 1 and 3 were classed as "1") and for trait type 3, 2 unique traits (trait richness) were observed; thus, 2+1+2=5.
>
> and so on... so at site 2, all three species were observed...
>
> For trait type 1, 3 unique traits were observed (1, 2, 3), for trait type 2, 2 unique traits were observed (2, 4, 2) and for trait type 3, 2 unique traits were observed (5, 1, 1). So, for site 2, trait richness is 7 (3+2+2) traits.
>
> I hope this helps to explain, please let me know if you need any further information,
>
> Ben Gillespie
> Research Postgraduate
>
> School of Geography
> University of Leeds
> Leeds
> LS2 9JT
>
> Tel: +44(0)113 34 33345
> Mob: +44(0)770 868 7641
> http://www.geog.leeds.ac.uk/
> ________________________________________
> From: arun [smartpink111 at yahoo.com]
> Sent: 24 September 2012 19:36
> To: Benjamin Gillespie
> Subject: Re: [R] Script to count unique values from two linked matricies
>
> HI Ben,
>
> Sorry,I couldn't understand how you counted the trait richness.  Could you elaborate?
> A.K.
>
>
> ----- Original Message -----
> From: benrgillespie <gybrg at leeds.ac.uk>
> To: r-help at r-project.org
> Cc:
> Sent: Monday, September 24, 2012 7:47 AM
> Subject: [R] Script to count unique values from two linked matricies
>
> I hope you can help with this one.
>
> I have two matricies:
>
> 1. A species abundance matrix:
>
> <http://r.789695.n4.nabble.com/file/n4643979/2species_matrix.jpg>
>
> 2. A species trait score matrix:
>
> <http://r.789695.n4.nabble.com/file/n4643979/2trait_matrix.jpg>
>
> The trait matrix lists trait scores for each species as listed in the
> species abundance matrix.
>
> I would like to create a script that would effectively count the unique
> traits (trait richness) for each site and produce an output like this:
>
> <http://r.789695.n4.nabble.com/file/n4643979/trait_richness.jpg>
>
> Firstly, is this possible in R? Secondly, if so, how would you go about
> writing a script to achieve my aim?
>
> Many thanks in advance, please let me know if you need further information.
>
> Ben Gillespie (Research Postgraduate)
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Script-to-count-unique-values-from-two-linked-matricies-tp4643979.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list