Hello,

Maybe something like the following.

res <- different(mat["B",], mat["E",])
res[res]


Rui Barradas

Em 27-09-2013 23:11, Elaine Kuo escreveu:
Thanks Rui and Arun.
Both worked well.

One more question, what shall I add for Rui's code if only the comparison
of TRUE (no FALSE) to be shown in the result?

Elaine

code
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",])


On Fri, Sep 27, 2013 at 9:31 PM, arun <smartpink...@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 <ruipbarra...@sapo.pt>
To: Elaine Kuo <elaine.kuo...@gmail.com>
Cc: "r-help@r-project.org" <r-help@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@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.

Reply via email to