[R] Compare species presence and absence between sites

arun smartpink111 at yahoo.com
Sat Sep 28 01:39:17 CEST 2013



Elaine,
Try:
set.seed(248)
 mat1<- matrix(sample(0:1,5*100,replace=TRUE),ncol=100,dimnames=list( c("Hokkaido","Honshu","Shikoku","Kyushu","Amami")
,paste0("D",sprintf("%03d",1:100))) )
##to change
 dat<- expand.grid(rownames(mat1),rownames(mat1),stringsAsFactors=FALSE)
dat1<- dat[!paste0(dat[,1],dat[,2])%in% paste0(rownames(mat1),rownames(mat1)),]

#same code:
lst1<- lapply(seq_len(nrow(dat1)),function(i) {x<-mat1[unlist(dat1[i,]),]; which(different(x[1,],x[2,]))}) #using Rui's function
 names(lst1)<- paste(dat1[,1],dat1[,2],sep="_")
A.K.

________________________________
From: Elaine Kuo <elaine.kuo.tw at gmail.com>
To: arun <smartpink111 at yahoo.com> 
Sent: Friday, September 27, 2013 6:17 PM
Subject: Re: [R] Compare species presence and absence between sites



Hello Arun, 

Thanks for the code, but I have problem replacing Island A-D with real names.
Please kindly indicate where to change in the code.
The real names include Hokkaido, Honshu, Shikoku, Kyushu, and Amami.
Thanks again.

Elaine



On Fri, Sep 27, 2013 at 9:31 PM, arun <smartpink111 at yahoo.com> wrote:

Just to add:
>
>If you wanted the difference of every combination of rows:
>set.seed(248)
> mat1<- matrix(sample(0:1,5*100,replace=TRUE),ncol=100,dimnames=list(LETTERS[1:5],paste0("D",sprintf("%03d",1:100))) )
> dat<-expand.grid(LETTERS[1:5],LETTERS[1:5],stringsAsFactors=FALSE)
>dat1<-dat[!paste0(dat[,1],dat[,2]) %in% paste0(LETTERS[1:5],LETTERS[1:5]),]
> lst1<- lapply(seq_len(nrow(dat1)),function(i) {x<-mat1[unlist(dat1[i,]),]; which(different(x[1,],x[2,]))}) #using Rui's function
>names(lst1)<- paste(dat1[,1],dat1[,2],sep="_")
>
>
>A.K.
>
>
>
>
>----- Original Message -----
>From: Rui Barradas <ruipbarradas at sapo.pt>
>To: Elaine Kuo <elaine.kuo.tw at gmail.com>
>Cc: "r-help at r-project.org" <r-help at r-project.org>
>Sent: Friday, September 27, 2013 7:39 AM
>Subject: Re: [R] Compare species presence and absence between sites
>
>Hello,
>
>Something like this?
>
>
>different <- function(x, y) x == 1 & y == 0
>
>set.seed(7054)
>mat <- matrix(sample(0:1, 500, TRUE), nrow = 5)
>rownames(mat) <- LETTERS[1:5]
>colnames(mat) <- sprintf("D%03d", 1:100)
>
>different(mat["B",], mat["E",])
>
>
>
>Hope this helps,
>
>Rui Barradas
>
>Em 27-09-2013 11:48, Elaine Kuo escreveu:
>> Dear List,
>>
>>
>>
>> I want to compare the presence and absence of bird species based on the
>> sites in a matrix.
>>
>> The matrix has 5 rows for Island A, B, C, D, and E.
>>
>> It has 100 columns for bird species D001-D100.
>>
>> In each cell of the matrix,
>>
>> the presence-absence of bird species will be recorded as 1 or 0.
>>
>> (For example, if species D001 is found on Island D,
>>
>> the matrix cell of species D001 and Island D will be 1.)
>>
>>
>>
>> Now I want to know the different bird species between Island B and E.
>>
>> In other words, I would like to find out bird species present (1) on Island
>> B but absent (0)on island E, and vice versa (absent (0) on Island B but
>> present (1)on island E).
>>
>>
>>
>> Please kindly advise how to code the purpose above.
>>
>> Thank you in advance.
>>
>>
>>
>> Elaine
>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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