[R] Compare species presence and absence between sites
arun
smartpink111 at yahoo.com
Sat Sep 28 16:31:58 CEST 2013
I couldn't get the number you showed below each column number. How did you get that one?
library(XLConnect)
wb<- loadWorkbook("is_matrix1.xls")
dataRM<- readWorksheet(wb,sheet="is_matrix",rownames=1)
mat1<- as.matrix(dataRM)
different <- function(x, y) x == 1 & y == 0
dat<- expand.grid(rownames(mat1),rownames(mat1),stringsAsFactors=FALSE)
dat1<- dat[!paste0(dat[,1],dat[,2])%in% paste0(rownames(mat1),rownames(mat1)),]
lst1<- lapply(seq_len(nrow(dat1)),function(i) {x<-mat1[unlist(dat1[i,]),]; which(different(x[1,],x[2,]))})
names(lst1)<- paste(dat1[,1],dat1[,2],sep="_")
tail(lst1,1)
#$New_Guinea_Australia
#D2781 D2782 D2784 D2785 D2786 D2787 D2796 D0005 D0006
# 1 2 4 5 6 7 8 9 10 #indicates the column index
#compare that with:
tail(lapply(seq_len(nrow(dat1)),function(i) {x<-mat1[unlist(dat1[i,]),];x}),1)
[[1]]
D2781 D2782 D2783 D2784 D2785 D2786 D2787 D2796 D0005 D0006 D0008
New_Guinea 1 1 0 1 1 1 1 1 1 1 0
Australia 0 0 0 0 0 0 0 0 0 0 0
D0009 D0010 D0011 D0012 D3394 D3395 D3396 D3397 D3398 D3399 D3400
New_Guinea 0 0 0 0 0 0 0 0 0 0 0
Australia 0 0 0 0 0 0 0 0 0 0 0
D3401
New_Guinea 0
Australia 0
#If you wanted the actual columns that satify the condition:
lst2<- lapply(seq_len(nrow(dat1)),function(i) {x<-mat1[unlist(dat1[i,]),]; indx<- different(x[1,],x[2,]);x[,indx,drop=FALSE]})
tail(lst2,1)
#[[1]]
# D2781 D2782 D2784 D2785 D2786 D2787 D2796 D0005 D0006
#New_Guinea 1 1 1 1 1 1 1 1 1
#Australia 0 0 0 0 0 0 0 0 0
A.K.
________________________________
From: Elaine Kuo <elaine.kuo.tw at gmail.com>
To: arun <smartpink111 at yahoo.com>
Sent: Saturday, September 28, 2013 12:23 AM
Subject: Re: [R] Compare species presence and absence between sites
Hello Arun,
Thanks for the immediate response.
Please kindly find the attached file.
For example, under D2202, it is 3402, not 2202.
Please kindly advise if this is a misunderstanding.
Thanks
Elaine
On Sat, Sep 28, 2013 at 11:01 AM, arun <smartpink111 at yahoo.com> wrote:
>
>
>Hi,
>I didn't understand the problem. Could you please specify what you meant by the "species ID number and the number"?
>Tx
>
>________________________________
>From: Elaine Kuo <elaine.kuo.tw at gmail.com>
>To: arun <smartpink111 at yahoo.com>
>Sent: Friday, September 27, 2013 10:51 PM
>
>Subject: Re: [R] Compare species presence and absence between sites
>
>
>
>
>sorry,
>I meant that the species ID number and the number are not the same.
>Need more sleep :P
>
>Elaine
>
>
>
>On Sat, Sep 28, 2013 at 10:50 AM, Elaine Kuo <elaine.kuo.tw at gmail.com> wrote:
>
>Dear arun,
>>
>>
>>Thanks for the code.
>>
>>
>>However, the number below each species ID is not identical.
>>Please kindly find attached file (I sent you once.) for test help.
>>Thanks again.
>>
>>
>>Elaine
>>
>>
>>
>>
>>Code
>>
>>
>>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.
>>
>>
>>
>>On Sat, Sep 28, 2013 at 7:39 AM, arun <smartpink111 at yahoo.com> wrote:
>>
>>
>>>
>>>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