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> occ.data <- read.table("Occ_03.csv", header=TRUE,sep=",",na.strings=TRUE) > occ.data[1:20,]year Ruta Point Site specie Pres Rep 1 2003 202 3 2021 MICH 1 3 2 2003 202 4 2021 MICH 1 4 3 2003 202 1 2021 MISA 1 1 4 2003 202 1 2021 MOBO 1 1 5 2003 202 2 2021 MOBO 1 2 6 2003 202 3 2021 MOBO 1 3 7 2003 202 4 2021 MOBO 1 4 8 2003 202 1 2021 SILU 1 1 9 2003 202 4 2021 SILU 1 4 10 2003 202 5 2021 TYSA 1 5 11 2003 202 1 2021 ZEAU 1 1 12 2003 202 3 2021 ZEAU 1 3 13 2003 202 4 2021 ZEAU 1 4 14 2003 202 5 2021 ZEAU 1 5 15 2003 202 1 2021 ZOCA 1 1 16 2003 202 4 2021 ZOCA 1 4 17 2003 202 5 2021 ZOCA 1 5 18 2003 202 10 2022 MICH 1 5 19 2003 202 7 2022 MISA 1 2 20 2003 202 8 2022 MISA 1 3> > ###Reshape the data using the R package "reshape" > library(reshape) > > all.melt=melt(occ.data,id.var=c("specie", "Site", "Rep", "year"),measure.var="Pres")> y=cast(all.melt, Site ~ Rep ~ specie ~ year) > > y[is.na(y)] <- 0 > > y[1:10,,1,], , year = 2003 Rep Site 1 2 3 4 5 1021 0 0 0 0 0 1022 0 0 0 0 0 1023 0 0 0 0 0 1024 0 0 0 0 0 1025 0 0 0 0 0 1026 0 0 0 0 0 2021 0 0 0 0 0 2022 0 0 0 0 0 2023 0 0 0 0 0 2024 0 0 0 0 0 , , year = 2004 Rep Site 1 2 3 4 5 1021 0 0 0 0 1 1022 1 0 0 0 0 1023 0 0 0 0 0 1024 0 0 0 0 0 1025 0 0 0 0 0 1026 0 0 0 0 0 2021 0 0 0 0 0 2022 0 0 0 0 0 2023 0 0 0 0 0 2024 0 0 0 0 0 [[alternative HTML version deleted]]
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> occ.data <- read.table("Occ_03.csv", header=TRUE,sep=",",na.strings=TRUE) > occ.data[1:20,]? ? year Ruta Point Site specie Pres Rep 1? 2003? ? ? 202? ? 3 2021? ? MICH? ? 1? 3 2? 2003? ? ? 202? ? 4 2021? ? MICH? ? 1? 4 3? 2003? ? ? 202? ? 1 2021? ? MISA? ? 1? 1 4? 2003? ? ? 202? ? 1 2021? ? MOBO? ? 1? 1 5? 2003? ? ? 202? ? 2 2021? ? MOBO? ? 1? 2 6? 2003? ? ? 202? ? 3 2021? ? MOBO? ? 1? 3 7? 2003? ? ? 202? ? 4 2021? ? MOBO? ? 1? 4 8? 2003? ? ? 202? ? 1 2021? ? SILU? ? 1? 1 9? 2003? ? ? 202? ? 4 2021? ? SILU? ? 1? 4 10 2003? ? ? 202? ? 5 2021? ? TYSA? ? 1? 5 11 2003? ? ? 202? ? 1 2021? ? ZEAU? ? 1? 1 12 2003? ? ? 202? ? 3 2021? ? ZEAU? ? 1? 3 13 2003? ? ? 202? ? 4 2021? ? ZEAU? ? 1? 4 14 2003? ? ? 202? ? 5 2021? ? ZEAU? ? 1? 5 15 2003? ? ? 202? ? 1 2021? ? ZOCA? ? 1? 1 16 2003? ? ? 202? ? 4 2021? ? ZOCA? ? 1? 4 17 2003? ? ? 202? ? 5 2021? ? ZOCA? ? 1? 5 18 2003? ? ? 202? ? 10 2022? ? MICH? ? 1? 5 19 2003? ? ? 202? ? 7 2022? ? MISA? ? 1? 2 20 2003? ? ? 202? ? 8 2022? ? MISA? ? 1? 3> > ###Reshape the data using the R package "reshape" > library(reshape) > > all.melt=melt(occ.data,id.var=c("specie", "Site", "Rep", "year"),measure.var="Pres")> y=cast(all.melt, Site ~ Rep ~ specie ~ year) > > y[is.na(y)] <- 0 > > y[1:10,,1,], , year = 2003 ? ? ? Rep Site? 1 2 3 4 5 ? 1021 0 0 0 0 0 ? 1022 0 0 0 0 0 ? 1023 0 0 0 0 0 ? 1024 0 0 0 0 0 ? 1025 0 0 0 0 0 ? 1026 0 0 0 0 0 ? 2021 0 0 0 0 0 ? 2022 0 0 0 0 0 ? 2023 0 0 0 0 0 ? 2024 0 0 0 0 0 , , year = 2004 ? ? ? Rep Site? 1 2 3 4 5 ? 1021 0 0 0 0 1 ? 1022 1 0 0 0 0 ? 1023 0 0 0 0 0 ? 1024 0 0 0 0 0 ? 1025 0 0 0 0 0 ? 1026 0 0 0 0 0 ? 2021 0 0 0 0 0 ? 2022 0 0 0 0 0 ? 2023 0 0 0 0 0 ? 2024 0 0 0 0 0 ??? [[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.
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> occ.data <- read.table("Occ_03.csv", header=TRUE,sep=",",na.strings=TRUE) > occ.data[1:20,]? ? year Ruta Point Site specie Pres Rep 1? 2003? ? ? 202? ? 3 2021? ? MICH? ? 1? 3 2? 2003? ? ? 202? ? 4 2021? ? MICH? ? 1? 4 3? 2003? ? ? 202? ? 1 2021? ? MISA? ? 1? 1 4? 2003? ? ? 202? ? 1 2021? ? MOBO? ? 1? 1 5? 2003? ? ? 202? ? 2 2021? ? MOBO? ? 1? 2 6? 2003? ? ? 202? ? 3 2021? ? MOBO? ? 1? 3 7? 2003? ? ? 202? ? 4 2021? ? MOBO? ? 1? 4 8? 2003? ? ? 202? ? 1 2021? ? SILU? ? 1? 1 9? 2003? ? ? 202? ? 4 2021? ? SILU? ? 1? 4 10 2003? ? ? 202? ? 5 2021? ? TYSA? ? 1? 5 11 2003? ? ? 202? ? 1 2021? ? ZEAU? ? 1? 1 12 2003? ? ? 202? ? 3 2021? ? ZEAU? ? 1? 3 13 2003? ? ? 202? ? 4 2021? ? ZEAU? ? 1? 4 14 2003? ? ? 202? ? 5 2021? ? ZEAU? ? 1? 5 15 2003? ? ? 202? ? 1 2021? ? ZOCA? ? 1? 1 16 2003? ? ? 202? ? 4 2021? ? ZOCA? ? 1? 4 17 2003? ? ? 202? ? 5 2021? ? ZOCA? ? 1? 5 18 2003? ? ? 202? ? 10 2022? ? MICH? ? 1? 5 19 2003? ? ? 202? ? 7 2022? ? MISA? ? 1? 2 20 2003? ? ? 202? ? 8 2022? ? MISA? ? 1? 3> > ###Reshape the data using the R package "reshape" > library(reshape) > > all.melt=melt(occ.data,id.var=c("specie", "Site", "Rep", "year"),measure.var="Pres")> y=cast(all.melt, Site ~ Rep ~ specie ~ year) > > y[is.na(y)] <- 0 > > y[1:10,,1,], , year = 2003 ? ? ? Rep Site? 1 2 3 4 5 ? 1021 0 0 0 0 0 ? 1022 0 0 0 0 0 ? 1023 0 0 0 0 0 ? 1024 0 0 0 0 0 ? 1025 0 0 0 0 0 ? 1026 0 0 0 0 0 ? 2021 0 0 0 0 0 ? 2022 0 0 0 0 0 ? 2023 0 0 0 0 0 ? 2024 0 0 0 0 0 , , year = 2004 ? ? ? Rep Site? 1 2 3 4 5 ? 1021 0 0 0 0 1 ? 1022 1 0 0 0 0 ? 1023 0 0 0 0 0 ? 1024 0 0 0 0 0 ? 1025 0 0 0 0 0 ? 1026 0 0 0 0 0 ? 2021 0 0 0 0 0 ? 2022 0 0 0 0 0 ? 2023 0 0 0 0 0 ? 2024 0 0 0 0 0 ??? [[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.
On 01/05/2013 10:15 AM, Andrea Goijman wrote: ... Hi Andrea, This may be what you want: occ.data<-data.frame(year=rep(2003,20),Ruta=rep(202,20), + Point=c(3,4,1,1,2,3,4,1,4,5,1,3,4,5,1,4,5,10,7,8), + Site=c(rep(2021,17),rep(2022,3)), + specie=c("MICH","MICH","MISA","MOBO","MOBO","MOBO","MOBO", + "SILU","SILU","TYSA","ZEAU","ZEAU","ZEAU","ZEAU","ZOCA","ZOCA", + "ZOCA","MICH","MISA","MISA"),Pres=rep(1,20), + Rep=c(3,4,1,1,2,3,4,1,4,5,1,3,4,5,1,4,5,5,2,3)) > library(prettyR) > stretch_df(occ.data,idvar="specie",to.stretch="Rep", > ordervar="Pres",include.ordervar=FALSE) specie year Ruta Point Site Rep_1 Rep_2 Rep_3 Rep_4 1 MICH 2003 202 3 2021 3 4 5 NA 2 MISA 2003 202 1 2021 1 2 3 NA 3 MOBO 2003 202 1 2021 1 2 3 4 4 SILU 2003 202 1 2021 1 4 NA NA 5 TYSA 2003 202 5 2021 5 NA NA NA 6 ZEAU 2003 202 1 2021 1 3 4 5 7 ZOCA 2003 202 1 2021 1 4 5 NA Jim