[R] how to combine presence only data sets to one presence/absence table

Chuck Cleland ccleland at optonline.net
Wed Jul 18 13:03:55 CEST 2007


Patrick Zimmermann wrote:
> Problem: I have a Set of samples each with a list of observed species
> (presence only).
> Data is stored in a excel spreadsheet and the columns (spl) have
> different numbers of observations (spcs).
> Now I want to organize the data in a species by sample matrix with
> presence/absence style in R.
> 
> data style (in excel):
> 
> spl_A	spl_B	spl_C
> spcs1	spcs1	spcs2
> spcs2	spcs3	spcs3
> spcs4		spcs5
> spcs5
> 
> desired style:
> 
> 	spl_A	spl_B	spl_C
> spcs1	1	1	0
> spcs2	1	0	1
> spcs3	0	1	1
> .
> .
> .
> 
> How and in which form do I import the data to R?
> (read.table() seems not to be appropriate, as data is not organized as a table)
> 
> How can I create the species by sample matrix?

  I'm not going to tackle how to read in the Excel data, but assuming
you had several vectors of species names gather together in a list, you
could construct a presence/absence data frame or matrix as follows:

spl_A <- c("spcs1","spcs2","spcs4","spcs5")
spl_B <- c("spcs1","spcs3")
spl_C <- c("spcs2","spcs3","spcs5")

mylist <- list(spl_A = spl_A, spl_B = spl_B, spl_C = spl_C)

mymat <- sapply(mylist,
          function(x){as.numeric(sort(unique(unlist(mylist))) %in% x)})

rownames(mymat) <- sort(unique(unlist(mylist)))

mymat
      spl_A spl_B spl_C
spcs1     1     1     0
spcs2     1     0     1
spcs3     0     1     1
spcs4     1     0     0
spcs5     1     0     1

> Thanks for any help,
> Patrick Zimmermann
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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. 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list