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 <smartpink...@yahoo.com> 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 <agoij...@cnia.inta.gov.ar>
> To: arun <smartpink...@yahoo.com>
> Cc: Rui Barradas <ruipbarra...@sapo.pt>; R help <r-help@r-project.org>
> 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 <smartpink...@yahoo.com> 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 <ruipbarra...@sapo.pt>
> >To: agoijman <agoij...@cnia.inta.gov.ar>
> >Cc: r-help@r-project.org
> >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.
> >>
> >> ______________________________________________
> >> R-help@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@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.
> >
> >
>
>
> --
>
> ---Lic. Andrea Paula Goijman
> Grupo Ecología y Gestión Ambiental de la Biodiversidad
> IRB - INTA Castelar, Argentina
> agoij...@cnia.inta.gov.arhttp://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
> andre...@uga.edu
>



-- 
---
Lic. Andrea Paula Goijman
Grupo Ecología y Gestión Ambiental de la Biodiversidad
IRB - INTA Castelar, Argentina
agoij...@cnia.inta.gov.ar
 <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
andre...@uga.edu

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to