Hi,
You could also use ?dcast()
occ.data1<-occ.data[,-c(2:3)]
library(reshape2)
res1<-dcast(occ.data1,year+Site+specie+Pres~Rep,value.var="Rep")
names(res1)[grep("[0-9]",names(res1))]<-paste("Rep",1:5,sep="")
res1[,-c(1:4)]<-sapply(res1[,-c(1:4)],function(x) as.integer(is.na(x)))
res1
#? year Site specie Pres Rep1 Rep2 Rep3 Rep4 Rep5
#1 2003 2021?? MICH??? 1??? 1??? 1??? 0??? 0??? 1
#2 2003 2021?? MISA??? 1??? 0??? 1??? 1??? 1??? 1
#3 2003 2021?? MOBO??? 1??? 0??? 0??? 0??? 0??? 1
#4 2003 2021?? SILU??? 1??? 0??? 1??? 1??? 0??? 1
#5 2003 2021?? TYSA??? 1??? 1??? 1??? 1??? 1??? 0
#6 2003 2021?? ZEAU??? 1??? 0??? 1??? 0??? 0??? 0
#7 2003 2021?? ZOCA??? 1??? 0??? 1??? 1??? 0??? 0
#8 2003 2022?? MICH??? 1??? 1??? 1??? 1??? 1??? 0
#9 2003 2022?? MISA??? 1??? 1??? 0??? 0??? 1??? 1
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