Andrea Goijman
2012-Oct-06 02:14 UTC
[R] Presence/ absence data from matrix to single column
I've been trying to reshape this database but haven't succeed at it. I tried using loops but can't get it right. I just want to reshape my database from this matrix, to the one below, with only one column of data. Year Route Point Sp1 Sp2 Sp3 2004 123 123-1 0 1 0 2004 123 123-2 0 1 1 2004 123 123-10 1 1 0 Year Route Point 2004 123 123-1 Sp1 0 2004 123 123-2 Sp1 0 2004 123 123-10 Sp1 1 2004 123 123-1 Sp2 1 2004 123 123-2 Sp2 1 2004 123 123-10 Sp2 1 2004 123 123-1 Sp3 0 2004 123 123-2 Sp3 1 2004 123 123-10 Sp3 0 [[alternative HTML version deleted]]
I've been trying to reshape this database but haven't succeed at it. I tried using loops but can't get it right. I just want to reshape my database from this matrix, to the one below, with only one column of data. Year Route Point Sp1 Sp2 Sp3 2004 123 123-1 0 1 0 2004 123 123-2 0 1 1 2004 123 123-10 1 1 0 What I want: Year Route Point 2004 123 123-1 Sp1 0 2004 123 123-2 Sp1 0 2004 123 123-10 Sp1 1 2004 123 123-1 Sp2 1 2004 123 123-2 Sp2 1 2004 123 123-10 Sp2 1 2004 123 123-1 Sp3 0 2004 123 123-2 Sp3 1 2004 123 123-10 Sp3 0 -- View this message in context: http://r.789695.n4.nabble.com/Presence-absence-data-from-matrix-to-single-column-tp4645271.html Sent from the R help mailing list archive at Nabble.com.
Hi John, Thanks for your comments. I have both packages.? I am using R 2.15.? May be reshape is out-of-date.? I don't load reshape2 (may be lazy to add 2 at the end) that much except when I need dcast() ....? I tried the code with only reshape2 loaded, and is getting the same result.? A.K. ----- Original Message ----- From: John Kane <jrkrideau at inbox.com> To: arun <smartpink111 at yahoo.com> Cc: Sent: Saturday, October 6, 2012 11:24 AM Subject: Re: [R] Presence/ absence data from matrix to single column I think reshape is out of date.? reshape2 has been out for about a year I think. John Kane Kingston ON Canada> -----Original Message----- > From: smartpink111 at yahoo.com > Sent: Sat, 6 Oct 2012 08:15:34 -0700 (PDT) > To:melt(dat1,id=c("Year","Route","Point")) > Subject: Re: [R] Presence/ absence data from matrix to single column > > Hi, > Try this: > dat1<-read.table(text=" > Year??? Route??? Point??? Sp1??? Sp2??? Sp3 > 2004??? 123??? 123-1??? 0??? 1??? 0 > 2004??? 123??? 123-2??? 0??? 1??? 1 > 2004??? 123??? 123-10??? 1??? 1??? 0 > ",header=TRUE,sep="",stringsAsFactors=FALSE) > > library(reshape) > melt(dat1,id=c("Year","Route","Point")) > ? Year Route? Point variable value > 1 2004?? 123? 123-1????? Sp1???? 0 > 2 2004?? 123? 123-2????? Sp1???? 0 > 3 2004?? 123 123-10????? Sp1???? 1 > 4 2004?? 123? 123-1????? Sp2???? 1 > 5 2004?? 123? 123-2????? Sp2???? 1 > 6 2004?? 123 123-10????? Sp2???? 1 > 7 2004?? 123? 123-1????? Sp3???? 0 > 8 2004?? 123? 123-2????? Sp3???? 1 > 9 2004?? 123 123-10????? Sp3???? 0 > A.K. > > > > > > ----- Original Message ----- > From: agoijman <agoijman at cnia.inta.gov.ar> > To: r-help at r-project.org > Cc: > Sent: Saturday, October 6, 2012 11:03 AM > Subject: [R] Presence/ absence data from matrix to single column > > I've been trying to reshape this database but haven't succeed at it. I > tried > using loops but can't get it right. I just want to reshape my database > from > this matrix, to the one below, with only one column of data. > > Year??? Route??? Point??? Sp1??? Sp2??? Sp3 > 2004??? 123??? 123-1??? 0??? 1??? 0 > 2004??? 123??? 123-2??? 0??? 1??? 1 > 2004??? 123??? 123-10??? 1??? 1??? 0 > > What I want: > > Year??? Route??? Point > 2004??? 123??? 123-1??? Sp1??? 0 > 2004??? 123??? 123-2??? Sp1??? 0 > 2004??? 123??? 123-10??? Sp1??? 1 > 2004??? 123??? 123-1??? Sp2??? 1 > 2004??? 123??? 123-2??? Sp2??? 1 > 2004??? 123??? 123-10??? Sp2??? 1 > 2004??? 123??? 123-1??? Sp3??? 0 > 2004??? 123??? 123-2??? Sp3??? 1 > 2004??? 123??? 123-10??? Sp3??? 0 > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Presence-absence-data-from-matrix-to-single-column-tp4645271.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. > > > ______________________________________________ > 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.____________________________________________________________ TRY FREE IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if5 Capture screenshots, upload images, edit and send them to your friends through IMs, post on Twitter?, Facebook?, MySpace?, LinkedIn? ? FAST!