[R] expanding a presence only dataset into presence/absence

arun smartpink111 at yahoo.com
Mon Apr 29 18:47:35 CEST 2013


Hi,

Your output dataset is bit confusing as it contains Sites that were not in the input.
Using your input dataset, I am getting this:


dat1<- read.table(text="
Species Site Date
a 1 1
b 1 1
b 1 2
c 1 3
",sep="",header=TRUE,stringsAsFactors=FALSE)
dat1$Present<- 1
dat2<-expand.grid(unique(dat1$Species),unique(dat1$Site),unique(dat1$Date))
 colnames(dat2)<- colnames(dat1)
res<-merge(dat1,dat2,by=c("Species","Site","Date"),all=TRUE)
res[is.na(res)]<- 0
 res<-res[order(res$Date),]
 res
#  Species Site Date Present
#1       a    1    1       1
#4       b    1    1       1
#7       c    1    1       0
#2       a    1    2       0
#5       b    1    2       1
#8       c    1    2       0
#3       a    1    3       0
#6       b    1    3       0
#9       c    1    3       1
A.K.





----- Original Message -----
From: Matthew Venesky <mvenesky at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Monday, April 29, 2013 11:12 AM
Subject: [R] expanding a presence only dataset into presence/absence

Hello,

I'm working with a very large dataset (250,000+ lines in its' current form)
that includes presence only data on various species (which is nested within
different sites and sampling dates). I need to convert this into a dataset
with presence/absence for each species. For example, I would like to expand
"My current data" to "Desired data":

My current data

Species Site Date
a 1 1
b 1 1
b 1 2
c 1 3

Desired data

Species Present Site Date
a 1 1 1
b 1 1 1
c 0 1 1
a 0 2 2
b 1 2 2
C 0 2 2
a 0 3 3
b 0 3 3
c 1 3 3

I've scoured the web, including Rseek and haven't found a resolution (and
note that a similar question was asked sometime in 2011 without an answer).
Does anyone have any thoughts? Thank you in advance.

--

Matthew D. Venesky, Ph.D.

Postdoctoral Research Associate,
Department of Integrative Biology,
The University of South Florida,
Tampa, FL 33620

Website: http://mvenesky.myweb.usf.edu/

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




More information about the R-help mailing list