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

2012-09-25 Thread Benjamin Gillespie
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 [smartpink...@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 gy...@leeds.ac.uk
To: r-help@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@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@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.


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

2012-09-25 Thread Heramb Gadgil
We can have something like this;

*Data1-data.frame(Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA))
Data2-data.frame(TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1))
*

*rownames(Data1)-paste(Species,1:3)*

*rownames(Data2)-paste(Species,1:3)*

 *User_Defined=function(dat1,dat2){c1=ncol(dat1)*

*out=NULL*

*dummy=lapply(1:c1,function(x){step1=dat1[,x]*

*step2=dat2[which(is.na(step1)==F),]*

*step3=length(unlist(apply(step2,2,unique)))*

*out-c(out,step3)*

*})*

*Final-data.frame(TraitRichness=out)*

*rownames(Final)-paste('Site',1:3)*

 *Final_Table-Final*

*}*

 *User_Defined(Data1,Data2)*

*Final_Table*


I hope this is what you need.

Best,
Heramb

On Tue, Sep 25, 2012 at 1:41 PM, Benjamin Gillespie gy...@leeds.ac.ukwrote:

 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 [smartpink...@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 gy...@leeds.ac.uk
 To: r-help@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@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@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.


[[alternative HTML version deleted]]

__
R-help@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.


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

2012-09-25 Thread PIKAL Petr
Hi

thanks for data. Probably others can come with better solution

with 

library(reshape)

you can put your data to better form

dat2.m-melt(dat2)
dat1.m-melt(dat1)

merge them
dat-merge(dat1.m, dat2.m, by=Species)


and finally compute required values

select one site
temp-dat[dat[,2]==Site3 !is.na(dat[,3]),]

and compute unique values
length(unlist(aggregate(temp$value.y, list(temp$variable.y), unique)$x))

It is not canned solution as I do not have much time to think it over, but if 
you do not have too much sites you could compute it in simple for cycle.

Regards
Petr


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Benjamin Gillespie
 Sent: Tuesday, September 25, 2012 10:11 AM
 To: arun; r-help@r-project.org
 Subject: Re: [R] Script to count unique values from two linked
 matricies
 
 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 [smartpink...@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 gy...@leeds.ac.uk
 To: r-help@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@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@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@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.


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

2012-09-25 Thread Rui Barradas

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 [smartpink...@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 gy...@leeds.ac.uk
To: r-help@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@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@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@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.


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

2012-09-25 Thread Benjamin Gillespie
Fantastic help - thanks guys.

Heramb, Pikal and Rui all came up with solutions.

I prefer Rui's as it seems the simplest.

I'm going to try and work through them to understand exactly what's going on at 
each stage.

Thanks again all,

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: Heramb Gadgil [heramb.gad...@gmail.com]
Sent: 25 September 2012 10:38
To: Benjamin Gillespie
Cc: arun; r-help@r-project.org
Subject: Re: [R] Script to count unique values from two linked matricies

We can have something like this;

Data1-data.frame(Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA))
Data2-data.frame(TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1))

rownames(Data1)-paste(Species,1:3)

rownames(Data2)-paste(Species,1:3)

User_Defined=function(dat1,dat2){c1=ncol(dat1)

out=NULL

dummy=lapply(1:c1,function(x){step1=dat1[,x]

step2=dat2[which(is.nahttp://is.na(step1)==F),]

step3=length(unlist(apply(step2,2,unique)))

out-c(out,step3)

})

Final-data.frame(TraitRichness=out)

rownames(Final)-paste('Site',1:3)

Final_Table-Final

}

User_Defined(Data1,Data2)

Final_Table


I hope this is what you need.

Best,
Heramb

On Tue, Sep 25, 2012 at 1:41 PM, Benjamin Gillespie 
gy...@leeds.ac.ukmailto:gy...@leeds.ac.uk wrote:
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 [smartpink...@yahoo.commailto:smartpink...@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 gy...@leeds.ac.ukmailto:gy...@leeds.ac.uk
To: r-help@r-project.orgmailto:r-help@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@r-project.orgmailto:R-help@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@r-project.orgmailto:R-help

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

2012-09-25 Thread Benjamin Gillespie
Excellent - thanks so much.

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 [smartpink...@yahoo.com]
Sent: 25 September 2012 14:22
To: Benjamin Gillespie
Cc: PIKAL Petr; Rui Barradas; Heramb Gadgil; R help
Subject: Re: [R] Script to count unique values from two linked matricies

Hi Ben,

Just a modification of Petr's solution with library(data.table):
dat1-data.frame(Species=paste0(Species,1:3),Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA))
 
dat2-data.frame(Species=paste0(Species,1:3),Trait1=1:3,Trait2=c(2,4,2),Trait3=c(5,1,1))
library(data.table)
library(reshape)
datm-merge(melt(dat1),melt(dat2),by=Species)
temp-list(data.table(datm[datm[,2]==Site1  
!is.na(datm[,3]),]),data.table(datm[datm[,2]==Site2  
!is.na(datm[,3]),]),data.table(datm[datm[,2]==Site3  !is.na(datm[,3]),]))
Trait_Richness-unlist(lapply(temp, function(x) 
sum(x[,list(length(unique(value.y))),list(variable.y)]$V1)))
 dat3-data.frame(Site=colnames(dat1)[2:4],Trait_Richness=Trait_Richness)
 dat3
#   Site Trait_Richness
#1 Site1  5
#2 Site2  7
#3 Site3  3
A.K.



- Original Message -
From: Benjamin Gillespie gy...@leeds.ac.uk
To: Heramb Gadgil heramb.gad...@gmail.com
Cc: arun smartpink...@yahoo.com; r-help@r-project.org r-help@r-project.org
Sent: Tuesday, September 25, 2012 8:56 AM
Subject: RE: [R] Script to count unique values from two linked matricies

Fantastic help - thanks guys.

Heramb, Pikal and Rui all came up with solutions.

I prefer Rui's as it seems the simplest.

I'm going to try and work through them to understand exactly what's going on at 
each stage.

Thanks again all,

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: Heramb Gadgil [heramb.gad...@gmail.com]
Sent: 25 September 2012 10:38
To: Benjamin Gillespie
Cc: arun; r-help@r-project.org
Subject: Re: [R] Script to count unique values from two linked matricies

We can have something like this;

Data1-data.frame(Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA))
Data2-data.frame(TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1))

rownames(Data1)-paste(Species,1:3)

rownames(Data2)-paste(Species,1:3)

User_Defined=function(dat1,dat2){c1=ncol(dat1)

out=NULL

dummy=lapply(1:c1,function(x){step1=dat1[,x]

step2=dat2[which(is.nahttp://is.na(step1)==F),]

step3=length(unlist(apply(step2,2,unique)))

out-c(out,step3)

})

Final-data.frame(TraitRichness=out)

rownames(Final)-paste('Site',1:3)

Final_Table-Final

}

User_Defined(Data1,Data2)

Final_Table


I hope this is what you need.

Best,
Heramb

On Tue, Sep 25, 2012 at 1:41 PM, Benjamin Gillespie 
gy...@leeds.ac.ukmailto:gy...@leeds.ac.uk wrote:
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 [smartpink...@yahoo.commailto:smartpink...@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 gy

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

2012-09-25 Thread Rui Barradas

Hello,

You must run the line defining function countTrait first in order to 
create it. Then run the rest.


Rui Barradas
Em 25-09-2012 13:42, Benjamin Gillespie escreveu:

Hi Rui, thanks, but I get this:


result - apply(presence, 2, function(x) apply(dat2[, -1]*x, 2, countTrait))

Error in match.fun(FUN) : object 'countTrait' not found

Any ideas what could be going wrong?

Thanks for your help,

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: Rui Barradas [ruipbarra...@sapo.pt]
Sent: 25 September 2012 10:30
To: Benjamin Gillespie
Cc: arun; r-help@r-project.org
Subject: Re: [R] Script to count unique values from two linked matricies

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 [smartpink...@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 gy...@leeds.ac.uk
To: r-help@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@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@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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide

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

2012-09-25 Thread arun
Hi Ben,

Just a modification of Petr's solution with library(data.table):
dat1-data.frame(Species=paste0(Species,1:3),Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA))
 dat2-data.frame(Species=paste0(Species,1:3),Trait1=1:3,Trait2=c(2,4,2),Trait3=c(5,1,1))
library(data.table)
library(reshape)
datm-merge(melt(dat1),melt(dat2),by=Species)
temp-list(data.table(datm[datm[,2]==Site1  
!is.na(datm[,3]),]),data.table(datm[datm[,2]==Site2  
!is.na(datm[,3]),]),data.table(datm[datm[,2]==Site3  !is.na(datm[,3]),]))
Trait_Richness-unlist(lapply(temp, function(x) 
sum(x[,list(length(unique(value.y))),list(variable.y)]$V1)))
 dat3-data.frame(Site=colnames(dat1)[2:4],Trait_Richness=Trait_Richness)
 dat3
#   Site Trait_Richness
#1 Site1  5
#2 Site2  7
#3 Site3  3
A.K.



- Original Message -
From: Benjamin Gillespie gy...@leeds.ac.uk
To: Heramb Gadgil heramb.gad...@gmail.com
Cc: arun smartpink...@yahoo.com; r-help@r-project.org r-help@r-project.org
Sent: Tuesday, September 25, 2012 8:56 AM
Subject: RE: [R] Script to count unique values from two linked matricies

Fantastic help - thanks guys.

Heramb, Pikal and Rui all came up with solutions.

I prefer Rui's as it seems the simplest.

I'm going to try and work through them to understand exactly what's going on at 
each stage.

Thanks again all,

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: Heramb Gadgil [heramb.gad...@gmail.com]
Sent: 25 September 2012 10:38
To: Benjamin Gillespie
Cc: arun; r-help@r-project.org
Subject: Re: [R] Script to count unique values from two linked matricies

We can have something like this;

Data1-data.frame(Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA))
Data2-data.frame(TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1))

rownames(Data1)-paste(Species,1:3)

rownames(Data2)-paste(Species,1:3)

User_Defined=function(dat1,dat2){c1=ncol(dat1)

out=NULL

dummy=lapply(1:c1,function(x){step1=dat1[,x]

step2=dat2[which(is.nahttp://is.na(step1)==F),]

step3=length(unlist(apply(step2,2,unique)))

out-c(out,step3)

})

Final-data.frame(TraitRichness=out)

rownames(Final)-paste('Site',1:3)

Final_Table-Final

}

User_Defined(Data1,Data2)

Final_Table


I hope this is what you need.

Best,
Heramb

On Tue, Sep 25, 2012 at 1:41 PM, Benjamin Gillespie 
gy...@leeds.ac.ukmailto:gy...@leeds.ac.uk wrote:
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 [smartpink...@yahoo.commailto:smartpink...@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 gy...@leeds.ac.ukmailto:gy...@leeds.ac.uk
To: r-help@r-project.orgmailto:r-help@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

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

2012-09-25 Thread Benjamin Gillespie
You're good!

Thanks again :)

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 [smartpink...@yahoo.com]
Sent: 25 September 2012 17:04
To: Benjamin Gillespie
Cc: R help
Subject: Re: [R] Script to count unique values from two linked matricies

Hi Ben,

No problem.

The code could be collapsed to 2 steps:
library(data.table)
library(reshape)
datm-merge(melt(dat1),melt(dat2),by=Species)
dat3-data.frame(unlist(lapply(lapply(split(datm,datm$variable.x),function(x) 
data.table(x[!is.na(x$value.x),])),function(x) 
sum(x[,list(length(unique(value.y))),list(variable.y)]$V1
 colnames(dat3)-Trait_Richness
dat3
#  Trait_Richness
#Site1  5
#Site2  7
#Site3  3
A.K.




- Original Message -
From: Benjamin Gillespie gy...@leeds.ac.uk
To: arun smartpink...@yahoo.com
Cc: PIKAL Petr petr.pi...@precheza.cz; Rui Barradas ruipbarra...@sapo.pt; 
Heramb Gadgil heramb.gad...@gmail.com; R help r-help@r-project.org
Sent: Tuesday, September 25, 2012 9:34 AM
Subject: RE: [R] Script to count unique values from two linked matricies

Excellent - thanks so much.

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 [smartpink...@yahoo.com]
Sent: 25 September 2012 14:22
To: Benjamin Gillespie
Cc: PIKAL Petr; Rui Barradas; Heramb Gadgil; R help
Subject: Re: [R] Script to count unique values from two linked matricies

Hi Ben,

Just a modification of Petr's solution with library(data.table):
dat1-data.frame(Species=paste0(Species,1:3),Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA))
dat2-data.frame(Species=paste0(Species,1:3),Trait1=1:3,Trait2=c(2,4,2),Trait3=c(5,1,1))
library(data.table)
library(reshape)
datm-merge(melt(dat1),melt(dat2),by=Species)
temp-list(data.table(datm[datm[,2]==Site1  
!is.na(datm[,3]),]),data.table(datm[datm[,2]==Site2  
!is.na(datm[,3]),]),data.table(datm[datm[,2]==Site3  !is.na(datm[,3]),]))
Trait_Richness-unlist(lapply(temp, function(x) 
sum(x[,list(length(unique(value.y))),list(variable.y)]$V1)))
dat3-data.frame(Site=colnames(dat1)[2:4],Trait_Richness=Trait_Richness)
dat3
#   Site Trait_Richness
#1 Site1  5
#2 Site2  7
#3 Site3  3
A.K.



- Original Message -
From: Benjamin Gillespie gy...@leeds.ac.uk
To: Heramb Gadgil heramb.gad...@gmail.com
Cc: arun smartpink...@yahoo.com; r-help@r-project.org r-help@r-project.org
Sent: Tuesday, September 25, 2012 8:56 AM
Subject: RE: [R] Script to count unique values from two linked matricies

Fantastic help - thanks guys.

Heramb, Pikal and Rui all came up with solutions.

I prefer Rui's as it seems the simplest.

I'm going to try and work through them to understand exactly what's going on at 
each stage.

Thanks again all,

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: Heramb Gadgil [heramb.gad...@gmail.com]
Sent: 25 September 2012 10:38
To: Benjamin Gillespie
Cc: arun; r-help@r-project.org
Subject: Re: [R] Script to count unique values from two linked matricies

We can have something like this;

Data1-data.frame(Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA))
Data2-data.frame(TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1))

rownames(Data1)-paste(Species,1:3)

rownames(Data2)-paste(Species,1:3)

User_Defined=function(dat1,dat2){c1=ncol(dat1)

out=NULL

dummy=lapply(1:c1,function(x){step1=dat1[,x]

step2=dat2[which(is.nahttp://is.na(step1)==F),]

step3=length(unlist(apply(step2,2,unique)))

out-c(out,step3)

})

Final-data.frame(TraitRichness=out)

rownames(Final)-paste('Site',1:3)

Final_Table-Final

}

User_Defined(Data1,Data2)

Final_Table


I hope this is what you need.

Best,
Heramb

On Tue, Sep 25, 2012 at 1:41 PM, Benjamin Gillespie 
gy...@leeds.ac.ukmailto:gy...@leeds.ac.uk wrote:
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

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

2012-09-25 Thread arun
Hi Ben,

No problem.

The code could be collapsed to 2 steps:
library(data.table)
library(reshape)
datm-merge(melt(dat1),melt(dat2),by=Species)
dat3-data.frame(unlist(lapply(lapply(split(datm,datm$variable.x),function(x) 
data.table(x[!is.na(x$value.x),])),function(x) 
sum(x[,list(length(unique(value.y))),list(variable.y)]$V1
 colnames(dat3)-Trait_Richness
dat3
#  Trait_Richness
#Site1  5
#Site2  7
#Site3  3
A.K.




- Original Message -
From: Benjamin Gillespie gy...@leeds.ac.uk
To: arun smartpink...@yahoo.com
Cc: PIKAL Petr petr.pi...@precheza.cz; Rui Barradas ruipbarra...@sapo.pt; 
Heramb Gadgil heramb.gad...@gmail.com; R help r-help@r-project.org
Sent: Tuesday, September 25, 2012 9:34 AM
Subject: RE: [R] Script to count unique values from two linked matricies

Excellent - thanks so much.

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 [smartpink...@yahoo.com]
Sent: 25 September 2012 14:22
To: Benjamin Gillespie
Cc: PIKAL Petr; Rui Barradas; Heramb Gadgil; R help
Subject: Re: [R] Script to count unique values from two linked matricies

Hi Ben,

Just a modification of Petr's solution with library(data.table):
dat1-data.frame(Species=paste0(Species,1:3),Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA))
dat2-data.frame(Species=paste0(Species,1:3),Trait1=1:3,Trait2=c(2,4,2),Trait3=c(5,1,1))
library(data.table)
library(reshape)
datm-merge(melt(dat1),melt(dat2),by=Species)
temp-list(data.table(datm[datm[,2]==Site1  
!is.na(datm[,3]),]),data.table(datm[datm[,2]==Site2  
!is.na(datm[,3]),]),data.table(datm[datm[,2]==Site3  !is.na(datm[,3]),]))
Trait_Richness-unlist(lapply(temp, function(x) 
sum(x[,list(length(unique(value.y))),list(variable.y)]$V1)))
dat3-data.frame(Site=colnames(dat1)[2:4],Trait_Richness=Trait_Richness)
dat3
#   Site Trait_Richness
#1 Site1              5
#2 Site2              7
#3 Site3              3
A.K.



- Original Message -
From: Benjamin Gillespie gy...@leeds.ac.uk
To: Heramb Gadgil heramb.gad...@gmail.com
Cc: arun smartpink...@yahoo.com; r-help@r-project.org r-help@r-project.org
Sent: Tuesday, September 25, 2012 8:56 AM
Subject: RE: [R] Script to count unique values from two linked matricies

Fantastic help - thanks guys.

Heramb, Pikal and Rui all came up with solutions.

I prefer Rui's as it seems the simplest.

I'm going to try and work through them to understand exactly what's going on at 
each stage.

Thanks again all,

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: Heramb Gadgil [heramb.gad...@gmail.com]
Sent: 25 September 2012 10:38
To: Benjamin Gillespie
Cc: arun; r-help@r-project.org
Subject: Re: [R] Script to count unique values from two linked matricies

We can have something like this;

Data1-data.frame(Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA))
Data2-data.frame(TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1))

rownames(Data1)-paste(Species,1:3)

rownames(Data2)-paste(Species,1:3)

User_Defined=function(dat1,dat2){c1=ncol(dat1)

out=NULL

dummy=lapply(1:c1,function(x){step1=dat1[,x]

step2=dat2[which(is.nahttp://is.na(step1)==F),]

step3=length(unlist(apply(step2,2,unique)))

out-c(out,step3)

})

Final-data.frame(TraitRichness=out)

rownames(Final)-paste('Site',1:3)

Final_Table-Final

}

User_Defined(Data1,Data2)

Final_Table


I hope this is what you need.

Best,
Heramb

On Tue, Sep 25, 2012 at 1:41 PM, Benjamin Gillespie 
gy...@leeds.ac.ukmailto:gy...@leeds.ac.uk wrote:
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

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

2012-09-24 Thread benrgillespie
Note, trait richness would be 5 and not 4 at site 1 perfectly
demonstrating why I want to create a script rather than manually count!





--
View this message in context: 
http://r.789695.n4.nabble.com/Script-to-count-unique-values-from-two-linked-matricies-tp4643979p4643980.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


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

2012-09-24 Thread Benjamin Gillespie
Hi Petr,

Please see the attached .csv files - perhaps these will help,

Thanks,

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: PIKAL Petr [petr.pi...@precheza.cz]
Sent: 24 September 2012 13:34
To: Benjamin Gillespie; r-help@r-project.org
Subject: RE: [R] Script to count unique values from two linked matricies

Hi

I have no access to Nabble so it is difficult to understand what do you want.

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of benrgillespie
 Sent: Monday, September 24, 2012 1:47 PM
 To: r-help@r-project.org
 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

Why not, everything is possible with R. Maybe you could provide data by 
dput(species).

Maybe you want something like ?table or ?aggregate.

Without data it is hard to say.

Regards
Petr



 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@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@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] Script to count unique values from two linked matricies

2012-09-24 Thread benrgillespie
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@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.


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

2012-09-24 Thread PIKAL Petr
Hi

I have no access to Nabble so it is difficult to understand what do you want.

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of benrgillespie
 Sent: Monday, September 24, 2012 1:47 PM
 To: r-help@r-project.org
 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

Why not, everything is possible with R. Maybe you could provide data by 
dput(species).

Maybe you want something like ?table or ?aggregate.

Without data it is hard to say.

Regards
Petr



 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@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@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.


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

2012-09-24 Thread benrgillespie
Richness should look like this:

http://r.789695.n4.nabble.com/file/n4644004/trait_richness.jpg 



--
View this message in context: 
http://r.789695.n4.nabble.com/Script-to-count-unique-values-from-two-linked-matricies-tp4643979p4644004.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.