Skip to content
Prev 322663 / 398500 Next

expanding a presence only dataset into presence/absence

I am sorry.? I forgot to update the code: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)[-4] #changed here
res<-merge(dat1,dat2,by=c("Species","Site","Date"),all=TRUE)
res[is.na(res)]<- 0
?res<-res[order(res$Date),]

row.names(res)<- 1:nrow(res)
res
#? Species Site Date Present
#1?????? a??? 1??? 1?????? 1
#2?????? b??? 1??? 1?????? 1
#3?????? c??? 1??? 1?????? 0
#4?????? a??? 1??? 2?????? 0
#5?????? b??? 1??? 2?????? 1
#6?????? c??? 1??? 2?????? 0
#7?????? a??? 1??? 3?????? 0
#8?????? b??? 1??? 3?????? 0
#9?????? c??? 1??? 3?????? 1
A.K.
Message-ID: <1367258703.43505.YahooMailNeo@web142603.mail.bf1.yahoo.com>
In-Reply-To: <CAMXzubEZtLb5qqD+QMvu7tPnUi0EjoREfJnAjUt-OL7X6nFTCw@mail.gmail.com>