Presence/ absence data from matrix to single column

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Presence/ absence data from matrix to single column

agoijman
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
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

arun kirshna
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 <[hidden email]>
To: [hidden email]
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.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

John Kane
In reply to this post by agoijman
Try the reshape2 package. You will probablly have to install the package.  install.packages("reshape2)

with your data as xx :
library(reshape2)
melt(xx, id =c("Year", "Route", "Point"))

seems to do what you want.

John Kane
Kingston ON Canada


> -----Original Message-----
> From: [hidden email]
> Sent: Sat, 6 Oct 2012 08:03:11 -0700 (PDT)
> To: [hidden email]
> 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.
>
> ______________________________________________
> [hidden email] 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.

____________________________________________________________
Share photos & screenshots in seconds...
TRY FREE IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if1
Works in all emails, instant messengers, blogs, forums and social networks.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

arun kirshna
In reply to this post by agoijman
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 <[hidden email]>
To: arun <[hidden email]>
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: [hidden email]
> 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 <[hidden email]>
> To: [hidden email]
> 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.
>
> ______________________________________________
> [hidden email] 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.
>
>
> ______________________________________________
> [hidden email] 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!

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

agoijman
In reply to this post by arun kirshna
Works fantastic!!! thank you SO much
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

Jim Lemon
In reply to this post by agoijman
On 10/07/2012 01:03 AM, agoijman wrote:

> 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
>
>
Hi agoijman,
You can do this using the rep_n_stack function.

adat<-data.frame(Year=rep(2004,3),Route=rep(123,3),
  Point=c("123-1","123-2","123-10"),Sp1=c(0,0,1),
  Sp2=c(1,1,1),Sp3=c(0,1,0))
library(prettyR)
rep_n_stack(adat,c("Sp1","Sp2","Sp3"),
  stack.names=c("Sp-names","Sp-values"))

Jim

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

David Carlson
Also reshape() will work:

adat<-data.frame(Year=rep(2004,3),Route=rep(123,3),
  Point=c("123-1","123-2","123-10"),Sp1=c(0,0,1),
  Sp2=c(1,1,1),Sp3=c(0,1,0))
reshape(adat, varying=4:6, v.name="Sp-value",
  times=c("Sp1", "Sp2", "Sp3"), idvar="Point",
  timevar="Sp-name", direction="long")

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Jim Lemon
> Sent: Saturday, October 06, 2012 7:36 PM
> To: agoijman
> Cc: [hidden email]
> Subject: Re: [R] Presence/ absence data from matrix to single column
>
> On 10/07/2012 01:03 AM, agoijman wrote:
> > 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
> >
> >
> Hi agoijman,
> You can do this using the rep_n_stack function.
>
> adat<-data.frame(Year=rep(2004,3),Route=rep(123,3),
>   Point=c("123-1","123-2","123-10"),Sp1=c(0,0,1),
>   Sp2=c(1,1,1),Sp3=c(0,1,0))
> library(prettyR)
> rep_n_stack(adat,c("Sp1","Sp2","Sp3"),
>   stack.names=c("Sp-names","Sp-values"))
>
> Jim
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

agoijman
The problem with that, is that I just wrote an example of my database, but I have around 250 species and more than 500 sites. In the approach you show me, it looks like I have to enter every species name and sites individually, right?
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

Rui Barradas
Hello,

I haven't been following this thread but apparently the answer to your
worries is no.
You can use a combination of names() and grep() to sort it out.
something like

#nms <- names(adat)
nms <- c("Year", "Route", "Point", paste0("Sp", 1:250))

pattern <- "^Sp[[:digit:]]+$"
whichCols <- grep(pattern, nms)
whichNames <- nms[whichCols]

reshape(..., varying = whichCols, times = whichNames, ...)


Hope this helps,

Rui Barradas
Em 07-10-2012 15:35, agoijman escreveu:

> The problem with that, is that I just wrote an example of my database, but I
> have around 250 species and more than 500 sites. In the approach you show
> me, it looks like I have to enter every species name and sites individually,
> right?
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Presence-absence-data-from-matrix-to-single-column-tp4645271p4645331.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

arun kirshna
Hi,
I guess you are not talking about the melt() method.
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)


#If all the Sp columns are located next to another as shown in your example dataset, then you can also try this:
name1<-unlist(strsplit(paste(colnames(dat1)[4:6],collapse=" ")," "))
reshape(dat1,varying=4:6,v.name="Sp-value",times=name1,timevar="Sp-name",idvar=c("Year","Route","Point"),direction="long")

A.K.






----- Original Message -----
From: Rui Barradas <[hidden email]>
To: agoijman <[hidden email]>
Cc: [hidden email]
Sent: Sunday, October 7, 2012 2:32 PM
Subject: Re: [R] Presence/ absence data from matrix to single column

Hello,

I haven't been following this thread but apparently the answer to your
worries is no.
You can use a combination of names() and grep() to sort it out.
something like

#nms <- names(adat)
nms <- c("Year", "Route", "Point", paste0("Sp", 1:250))

pattern <- "^Sp[[:digit:]]+$"
whichCols <- grep(pattern, nms)
whichNames <- nms[whichCols]

reshape(..., varying = whichCols, times = whichNames, ...)


Hope this helps,

Rui Barradas
Em 07-10-2012 15:35, agoijman escreveu:

> The problem with that, is that I just wrote an example of my database, but I
> have around 250 species and more than 500 sites. In the approach you show
> me, it looks like I have to enter every species name and sites individually,
> right?
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Presence-absence-data-from-matrix-to-single-column-tp4645271p4645331.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

David Carlson
In reply to this post by David Carlson
You should keep the context of your thread intact for those of us using
email instead of Nabble.

The locations were not listed individually anywhere in the reshape()
command. I listed Point as the id variable so reshape would use that to
create row names, but if you delete idvar="Point", reshape will give each
row a consecutive number followed by the species name or number.

I did list the species names individually in the times= argument to make
things a bit clearer since reshape() can be a confusing command at first.
But this would work as well:

reshape(adat, varying=4:6, v.name="Sp-value",
  times=names(adat)[4:6], idvar="Point",
  timevar="Sp-name", direction="long")

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> Date: Sun, 7 Oct 2012 07:35:42 -0700 (PDT)
> From: agoijman <[hidden email]>
> To: [hidden email]
> Subject: Re: [R] Presence/ absence data from matrix to single column
>
>
> The problem with that, is that I just wrote an example of my database, but
I
> have around 250 species and more than 500 sites. In the approach you show
> me, it looks like I have to enter every species name and sites
individually,
> right?
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Presence-absence-data-from-matrix-to-single-co
lumn-tp4645271p4645331.html
> Sent from the R help mailing list archive at Nabble.com.
>
> _____________________________________________
> [hidden email] 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.
> > -----Original Message-----
> > From: [hidden email] [mailto:r-help-bounces@r-
> > project.org] On Behalf Of David L Carlson
> > Sent: Saturday, October 06, 2012 10:25 PM
> > To: 'Jim Lemon'; 'agoijman'
> > Cc: [hidden email]
> > Subject: Re: [R] Presence/ absence data from matrix to single column
> >
> > Also reshape() will work:
> >
> > adat<-data.frame(Year=rep(2004,3),Route=rep(123,3),
> >   Point=c("123-1","123-2","123-10"),Sp1=c(0,0,1),
> >   Sp2=c(1,1,1),Sp3=c(0,1,0))
> > reshape(adat, varying=4:6, v.name="Sp-value",
> >   times=c("Sp1", "Sp2", "Sp3"), idvar="Point",
> >   timevar="Sp-name", direction="long")
> >
> > ----------------------------------------------
> > David L Carlson
> > Associate Professor of Anthropology
> > Texas A&M University
> > College Station, TX 77843-4352
> >
> >
> > > -----Original Message-----
> > > From: [hidden email] [mailto:r-help-bounces@r-
> > > project.org] On Behalf Of Jim Lemon
> > > Sent: Saturday, October 06, 2012 7:36 PM
> > > To: agoijman
> > > Cc: [hidden email]
> > > Subject: Re: [R] Presence/ absence data from matrix to single column
> > >
> > > On 10/07/2012 01:03 AM, agoijman wrote:
> > > > 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
> > > >
> > > >
> > > Hi agoijman,
> > > You can do this using the rep_n_stack function.
> > >
> > > adat<-data.frame(Year=rep(2004,3),Route=rep(123,3),
> > >   Point=c("123-1","123-2","123-10"),Sp1=c(0,0,1),
> > >   Sp2=c(1,1,1),Sp3=c(0,1,0))
> > > library(prettyR)
> > > rep_n_stack(adat,c("Sp1","Sp2","Sp3"),
> > >   stack.names=c("Sp-names","Sp-values"))
> > >
> > > Jim
> > >
> > > ______________________________________________
> > > [hidden email] 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.
> >
> > ______________________________________________
> > [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

agoijman
In reply to this post by arun kirshna
Ill try this one as well.

And I guess the one below is not going  to work, because all my species
have different names.

Thanks!

#nms <- names(adat)
nms <- c("Year", "Route", "Point", paste0("Sp", 1:250))

pattern <- "^Sp[[:digit:]]+$"
whichCols <- grep(pattern, nms)
whichNames <- nms[whichCols]

reshape(..., varying = whichCols, times = whichNames, ...)
On Sun, Oct 7, 2012 at 3:02 PM, arun <[hidden email]> wrote:

> Hi,
> I guess you are not talking about the melt() method.
> 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)
>
>
> #If all the Sp columns are located next to another as shown in your
> example dataset, then you can also try this:
> name1<-unlist(strsplit(paste(colnames(dat1)[4:6],collapse=" ")," "))
> reshape(dat1,varying=4:6,v.name
> ="Sp-value",times=name1,timevar="Sp-name",idvar=c("Year","Route","Point"),direction="long")
>
> A.K.
>
>
>
>
>
>
> ----- Original Message -----
> From: Rui Barradas <[hidden email]>
> To: agoijman <[hidden email]>
> Cc: [hidden email]
> Sent: Sunday, October 7, 2012 2:32 PM
> Subject: Re: [R] Presence/ absence data from matrix to single column
>
> Hello,
>
> I haven't been following this thread but apparently the answer to your
> worries is no.
> You can use a combination of names() and grep() to sort it out.
> something like
>
> #nms <- names(adat)
> nms <- c("Year", "Route", "Point", paste0("Sp", 1:250))
>
> pattern <- "^Sp[[:digit:]]+$"
> whichCols <- grep(pattern, nms)
> whichNames <- nms[whichCols]
>
> reshape(..., varying = whichCols, times = whichNames, ...)
>
>
> Hope this helps,
>
> Rui Barradas
> Em 07-10-2012 15:35, agoijman escreveu:
> > The problem with that, is that I just wrote an example of my database,
> but I
> > have around 250 species and more than 500 sites. In the approach you show
> > me, it looks like I have to enter every species name and sites
> individually,
> > right?
> >
> >
> >
> > --
> > View this message in context:
> http://r.789695.n4.nabble.com/Presence-absence-data-from-matrix-to-single-column-tp4645271p4645331.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>
>

--
---
Lic. Andrea Paula Goijman
Grupo Ecología y Gestión Ambiental de la Biodiversidad
IRB - INTA Castelar, Argentina
[hidden email]
 <http://inta.gob.ar/personas/goijman.andrea/>
http://inta.gob.ar/personas/goijman.andrea/

PhD Candidate
Georgia Cooperative Fish and Wildlife Research Unit
D.B. Warnell School of Forestry and Natural Resources
University of Georgia
Athens, GA 30602 USA
Tel. +706.206.4805
[hidden email]

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

Rui Barradas
Hello,

If all your species have different names, you can allways try one of
1. if the non-species follow a pattern, negate those columns and you'll
have the species columns.
2. are the species the last columns? Use positional referencing.

Rui Barradas
Em 08-10-2012 00:16, Andrea Goijman escreveu:

> Ill try this one as well.
>
> And I guess the one below is not going  to work, because all my species
> have different names.
>
> Thanks!
>
> #nms <- names(adat)
> nms <- c("Year", "Route", "Point", paste0("Sp", 1:250))
>
> pattern <- "^Sp[[:digit:]]+$"
> whichCols <- grep(pattern, nms)
> whichNames <- nms[whichCols]
>
> reshape(..., varying = whichCols, times = whichNames, ...)
> On Sun, Oct 7, 2012 at 3:02 PM, arun <[hidden email]> wrote:
>
>> Hi,
>> I guess you are not talking about the melt() method.
>> 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)
>>
>>
>> #If all the Sp columns are located next to another as shown in your
>> example dataset, then you can also try this:
>> name1<-unlist(strsplit(paste(colnames(dat1)[4:6],collapse=" ")," "))
>> reshape(dat1,varying=4:6,v.name
>> ="Sp-value",times=name1,timevar="Sp-name",idvar=c("Year","Route","Point"),direction="long")
>>
>> A.K.
>>
>>
>>
>>
>>
>>
>> ----- Original Message -----
>> From: Rui Barradas <[hidden email]>
>> To: agoijman <[hidden email]>
>> Cc: [hidden email]
>> Sent: Sunday, October 7, 2012 2:32 PM
>> Subject: Re: [R] Presence/ absence data from matrix to single column
>>
>> Hello,
>>
>> I haven't been following this thread but apparently the answer to your
>> worries is no.
>> You can use a combination of names() and grep() to sort it out.
>> something like
>>
>> #nms <- names(adat)
>> nms <- c("Year", "Route", "Point", paste0("Sp", 1:250))
>>
>> pattern <- "^Sp[[:digit:]]+$"
>> whichCols <- grep(pattern, nms)
>> whichNames <- nms[whichCols]
>>
>> reshape(..., varying = whichCols, times = whichNames, ...)
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>> Em 07-10-2012 15:35, agoijman escreveu:
>>> The problem with that, is that I just wrote an example of my database,
>> but I
>>> have around 250 species and more than 500 sites. In the approach you show
>>> me, it looks like I have to enter every species name and sites
>> individually,
>>> right?
>>>
>>>
>>>
>>> --
>>> View this message in context:
>> http://r.789695.n4.nabble.com/Presence-absence-data-from-matrix-to-single-column-tp4645271p4645331.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>> ______________________________________________
>> [hidden email] 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.
>>
>>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

arun kirshna
In reply to this post by agoijman


HI,

Sorry, I complicated a code where it was not required at all.
Just using colnames(dat1)[4:6] or names(dat1)[4:6] should work if the species columns are adjacent to each other.


reshape(dat1,varying=4:6,v.name="Sp-value",times=colnames(dat1)[4:6],timevar="Sp-name",idvar=c("Year","Route","Point"),direction="long")
#or
reshape(dat1,varying=4:6,v.name="Sp-value",times=names(dat1)[4:6],timevar="Sp-name",idvar=c("Year","Route","Point"),direction="long")
A.K.

________________________________
From: Andrea Goijman <[hidden email]>
To: arun <[hidden email]>
Cc: Rui Barradas <[hidden email]>; R help <[hidden email]>
Sent: Sunday, October 7, 2012 7:16 PM
Subject: Re: [R] Presence/ absence data from matrix to single column


Ill try this one as well.

And I guess the one below is not going  to work, because all my species have different names.

Thanks!

#nms <- names(adat)
nms <- c("Year", "Route", "Point", paste0("Sp", 1:250))

pattern <- "^Sp[[:digit:]]+$"
whichCols <- grep(pattern, nms)
whichNames <- nms[whichCols]

reshape(..., varying = whichCols, times = whichNames, ...)

On Sun, Oct 7, 2012 at 3:02 PM, arun <[hidden email]> wrote:

Hi,

>I guess you are not talking about the melt() method.
>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)
>
>
>#If all the Sp columns are located next to another as shown in your example dataset, then you can also try this:
>name1<-unlist(strsplit(paste(colnames(dat1)[4:6],collapse=" ")," "))
>reshape(dat1,varying=4:6,v.name="Sp-value",times=name1,timevar="Sp-name",idvar=c("Year","Route","Point"),direction="long")
>
>
>A.K.
>
>
>
>
>
>
>----- Original Message -----
>
>From: Rui Barradas <[hidden email]>
>To: agoijman <[hidden email]>
>Cc: [hidden email]
>Sent: Sunday, October 7, 2012 2:32 PM
>Subject: Re: [R] Presence/ absence data from matrix to single column
>
>Hello,
>
>I haven't been following this thread but apparently the answer to your
>worries is no.
>You can use a combination of names() and grep() to sort it out.
>something like
>
>#nms <- names(adat)
>nms <- c("Year", "Route", "Point", paste0("Sp", 1:250))
>
>pattern <- "^Sp[[:digit:]]+$"
>whichCols <- grep(pattern, nms)
>whichNames <- nms[whichCols]
>
>reshape(..., varying = whichCols, times = whichNames, ...)
>
>
>Hope this helps,
>
>Rui Barradas
>Em 07-10-2012 15:35, agoijman escreveu:
>> The problem with that, is that I just wrote an example of my database, but I
>> have around 250 species and more than 500 sites. In the approach you show
>> me, it looks like I have to enter every species name and sites individually,
>> right?
>>
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Presence-absence-data-from-matrix-to-single-column-tp4645271p4645331.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] 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.
>
>______________________________________________
>[hidden email] 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.
>
>


--

---Lic. Andrea Paula Goijman
Grupo Ecología y Gestión Ambiental de la Biodiversidad
IRB - INTA Castelar, Argentina
[hidden email]://inta.gob.ar/personas/goijman.andrea/


PhD Candidate
Georgia Cooperative Fish and Wildlife Research Unit

D.B. Warnell School of Forestry and Natural Resources
University of Georgia
Athens, GA 30602 USA
Tel. +706.206.4805
[hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Presence/ absence data from matrix to single column

agoijman
Hi all,

Thanks all for the great advices! I tried two of the approaches and
certainly Reshape, melt() gave me the best results in this case. I copy
below part of the code and output

#First approach
occ2012 <- read.table("Pres_Abs_punto_2012.csv",
header=TRUE,sep=",",na.strings=TRUE)
occ2012[1:5,]
occ2012.data<-melt(occ2012,id=c("Provincia","Región","Estrato","Ruta","Punto","Latitud","Longitud"))
occ2012.data[1:5,]

> occ2012[1:5,]
  Provincia Región Estrato Ruta Punto   Latitud  Longitud AASP AGCY AGRU
AGSP AGTH AIST AMBR
1         1      2       1    2     1 -31.01353 -62.64140    0    0    0
 0    0    0    0
2         1      2       1    2     2 -31.02265 -62.64152    0    0    0
 0    0    0    0
3         1      2       1    2     3 -31.03050 -62.64338    0    0    0
 0    0    0    0
4         1      2       1    2     4 -31.03872 -62.64528    0    0    0
 0    0    0    0
5         1      2       1    2     5 -31.04628 -62.64707    0    0    0
 0    0    0    0
  AMHU ANAN ANBA ANCA ANCH ANCO ANCY ANFA ANFL ANFU ANGE ANHE ANPL ANSI
ANSP ANVE ARAC ARCA
1    1    0    0    0    0    0    0    0    0    0    0    0    0    0
 0    0    0    0
2    0    0    0    0    0    0    0    0    0    0    0    0    0    0
 0    0    0    0
3    0    0    0    0    0    0    0    0    0    0    0    0    0    0
 0    0    0    0
4    0    0    0    0    0    0    0    0    0    0    0    0    0    0
 0    0    0    0
5    0    0    0    0    0    0    0    0    0    0    0    0    0    0
 0    0    0    0

>occ2012.data[1:5,]
  Provincia Región Estrato Ruta Punto   Latitud  Longitud variable value
1         1      2       1    2     1 -31.01353 -62.64140     AASP     0
2         1      2       1    2     2 -31.02265 -62.64152     AASP     0
3         1      2       1    2     3 -31.03050 -62.64338     AASP     0
4         1      2       1    2     4 -31.03872 -62.64528     AASP     0
5         1      2       1    2     5 -31.04628 -62.64707     AASP     0
>
#Second approach
occ2012<-read.table("Pres_Abs_punto_2012.csv",
header=TRUE,sep=",",na.strings=TRUE)

occ2012.data<-reshape(occ2012, varying=8:248, v.name="Sp-value",
times=names(occ2012)[8:248],
idvar=c("Provincia","Región","Estrato","Ruta","Punto","Latitud","Longitud"),

timevar="Sp-name", direction="long")

> occ2012.data[1:5,]
                                         Provincia Región Estrato Ruta
Punto   Latitud
1.2.1.2.1.-31.01353333.-62.6414.AASP             1      2       1    2
1 -31.01353
1.2.1.2.2.-31.02265.-62.64151667.AASP            1      2       1    2
2 -31.02265
1.2.1.2.3.-31.0305.-62.64338333.AASP             1      2       1    2
3 -31.03050
1.2.1.2.4.-31.03871667.-62.64528333.AASP         1      2       1    2
4 -31.03872
1.2.1.2.5.-31.04628333.-62.64706667.AASP         1      2       1    2
5 -31.04628
                                          Longitud SEGR SENI SESU SICI SIFL
SILU SISP Sp-name
1.2.1.2.1.-31.01353333.-62.6414.AASP     -62.64140    0    0    0    0    0
   0    0    AASP
1.2.1.2.2.-31.02265.-62.64151667.AASP    -62.64152    0    0    0    0    0
   0    0    AASP
1.2.1.2.3.-31.0305.-62.64338333.AASP     -62.64338    0    0    0    0    1
   0    0    AASP
1.2.1.2.4.-31.03871667.-62.64528333.AASP -62.64528    0    0    0    0    1
   0    0    AASP
1.2.1.2.5.-31.04628333.-62.64706667.AASP -62.64707    0    0    0    0    0
   0    0    AASP
                                         Sp-value
1.2.1.2.1.-31.01353333.-62.6414.AASP            0
1.2.1.2.2.-31.02265.-62.64151667.AASP           0
1.2.1.2.3.-31.0305.-62.64338333.AASP            0
1.2.1.2.4.-31.03871667.-62.64528333.AASP        0
1.2.1.2.5.-31.04628333.-62.64706667.AASP        0

On Sun, Oct 7, 2012 at 8:28 PM, arun <[hidden email]> wrote:

>
>
> HI,
>
> Sorry, I complicated a code where it was not required at all.
> Just using colnames(dat1)[4:6] or names(dat1)[4:6] should work if the
> species columns are adjacent to each other.
>
>
> reshape(dat1,varying=4:6,v.name
> ="Sp-value",times=colnames(dat1)[4:6],timevar="Sp-name",idvar=c("Year","Route","Point"),direction="long")
> #or
> reshape(dat1,varying=4:6,v.name
> ="Sp-value",times=names(dat1)[4:6],timevar="Sp-name",idvar=c("Year","Route","Point"),direction="long")
> A.K.
>
> ________________________________
> From: Andrea Goijman <[hidden email]>
> To: arun <[hidden email]>
> Cc: Rui Barradas <[hidden email]>; R help <[hidden email]>
> Sent: Sunday, October 7, 2012 7:16 PM
> Subject: Re: [R] Presence/ absence data from matrix to single column
>
>
> Ill try this one as well.
>
> And I guess the one below is not going  to work, because all my species
> have different names.
>
> Thanks!
>
> #nms <- names(adat)
> nms <- c("Year", "Route", "Point", paste0("Sp", 1:250))
>
> pattern <- "^Sp[[:digit:]]+$"
> whichCols <- grep(pattern, nms)
> whichNames <- nms[whichCols]
>
> reshape(..., varying = whichCols, times = whichNames, ...)
>
> On Sun, Oct 7, 2012 at 3:02 PM, arun <[hidden email]> wrote:
>
> Hi,
> >I guess you are not talking about the melt() method.
> >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)
> >
> >
> >#If all the Sp columns are located next to another as shown in your
> example dataset, then you can also try this:
> >name1<-unlist(strsplit(paste(colnames(dat1)[4:6],collapse=" ")," "))
> >reshape(dat1,varying=4:6,v.name
> ="Sp-value",times=name1,timevar="Sp-name",idvar=c("Year","Route","Point"),direction="long")
> >
> >
> >A.K.
> >
> >
> >
> >
> >
> >
> >----- Original Message -----
> >
> >From: Rui Barradas <[hidden email]>
> >To: agoijman <[hidden email]>
> >Cc: [hidden email]
> >Sent: Sunday, October 7, 2012 2:32 PM
> >Subject: Re: [R] Presence/ absence data from matrix to single column
> >
> >Hello,
> >
> >I haven't been following this thread but apparently the answer to your
> >worries is no.
> >You can use a combination of names() and grep() to sort it out.
> >something like
> >
> >#nms <- names(adat)
> >nms <- c("Year", "Route", "Point", paste0("Sp", 1:250))
> >
> >pattern <- "^Sp[[:digit:]]+$"
> >whichCols <- grep(pattern, nms)
> >whichNames <- nms[whichCols]
> >
> >reshape(..., varying = whichCols, times = whichNames, ...)
> >
> >
> >Hope this helps,
> >
> >Rui Barradas
> >Em 07-10-2012 15:35, agoijman escreveu:
> >> The problem with that, is that I just wrote an example of my database,
> but I
> >> have around 250 species and more than 500 sites. In the approach you
> show
> >> me, it looks like I have to enter every species name and sites
> individually,
> >> right?
> >>
> >>
> >>
> >> --
> >> View this message in context:
> http://r.789695.n4.nabble.com/Presence-absence-data-from-matrix-to-single-column-tp4645271p4645331.html
> >> Sent from the R help mailing list archive at Nabble.com.
> >>
> >> ______________________________________________
> >> [hidden email] 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.
> >
> >______________________________________________
> >[hidden email] 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.
> >
> >
>
>
> --
>
> ---Lic. Andrea Paula Goijman
> Grupo Ecología y Gestión Ambiental de la Biodiversidad
> IRB - INTA Castelar, Argentina
> [hidden email]://inta.gob.ar/personas/goijman.andrea/
>
>
> PhD Candidate
> Georgia Cooperative Fish and Wildlife Research Unit
>
> D.B. Warnell School of Forestry and Natural Resources
> University of Georgia
> Athens, GA 30602 USA
> Tel. +706.206.4805
> [hidden email]
>


--
---
Lic. Andrea Paula Goijman
Grupo Ecología y Gestión Ambiental de la Biodiversidad
IRB - INTA Castelar, Argentina
[hidden email]
 <http://inta.gob.ar/personas/goijman.andrea/>
http://inta.gob.ar/personas/goijman.andrea/

PhD Candidate
Georgia Cooperative Fish and Wildlife Research Unit
D.B. Warnell School of Forestry and Natural Resources
University of Georgia
Athens, GA 30602 USA
Tel. +706.206.4805
[hidden email]

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.