HI,
May be this helps:
?occ.data1<-occ.data[,-c(2:3)]
res<-reshape(occ.data1,direction="wide",idvar=c("specie","Site","Pres","year"),timevar="Rep",v.names="Rep")
res<-res[,c(1:4,7:8,5:6,9)]
res[grep("Rep",names(res))]<-apply(res[grep("Rep",names(res))],2, function(x) ifelse(is.na(x),0,1))?names(res)[grep("Rep",names(res))]<-gsub("[.]","",names(res)[grep("Rep",names(res))])
?row.names(res)<-1:nrow(res)
?res
#? year Site specie Pres Rep1 Rep2 Rep3 Rep4 Rep5
#1 2003 2021?? MICH??? 1??? 0??? 0??? 1??? 1??? 0
#2 2003 2021?? MISA??? 1??? 1??? 0??? 0??? 0??? 0
#3 2003 2021?? MOBO??? 1??? 1??? 1??? 1??? 1??? 0
#4 2003 2021?? SILU??? 1??? 1??? 0??? 0??? 1??? 0
#5 2003 2021?? TYSA??? 1??? 0??? 0??? 0??? 0??? 1
#6 2003 2021?? ZEAU??? 1??? 1??? 0??? 1??? 1??? 1
#7 2003 2021?? ZOCA??? 1??? 1??? 0??? 0??? 1??? 1
#8 2003 2022?? MICH??? 1??? 0??? 0??? 0??? 0??? 1
#9 2003 2022?? MISA??? 1??? 0??? 1??? 1??? 0??? 0
A.K.
----- Original Message -----
From: Andrea Goijman <agoijman at cnia.inta.gov.ar>
To: R help <r-help at r-project.org>
Cc:
Sent: Friday, January 4, 2013 6:15 PM
Subject: [R] help "reshaping" dataframe
List,
I want to reshape my data, but I'm not sure how to do it... it might be a
simple task, but don't know which package does this.
"occ.data" (see below) is how my original data are arranged, and I know
that with melt() I can reshape it like "y" (see below). However, I just
want to build a matrix like the "y" matrix, but with only 2 dimensions.
Something like this:
? ? year? ? Site? ? ? specie? Pres? Rep1? Rep2? Rep3? Rep4? Rep5
1? 2003? 2021? ? MICH? ? 1? ? ? ? ? 0? ? ? ? 0? ? ? ? ? 1? ? ? ? 1
0
3? 2003? 2021? ? MISA? ? 1? ? ? ? ? 1? ? ? ? 0? ? ? ? ? ? 0? ? ? ? 0
? 0
4? 2003? 2021? ? MOBO? ? 1? ? ? ? 1? ? ? ? 1? ? ? ? ? ? 0? ? ? ? 0
0
where "year" and "specie" are not another dimension, they are different
columns; and Rep is the other dimension