Capture data

classic Classic list List threaded Threaded
4 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Capture data

Allan Edelsparre-2
Hi all,

I have two data sets I want to merge. One has a vector with trap IDs and the vector from the other data set has the number of individuals captured at each trap site. I need to reverse the trap ID's within the trap ID vector in order to match the number trapped. Here is an example of how the vector looks like:

TrapID
A01
B01
C01
A02
B02
C02
A03
B03
C03

I need the vector to look like this

C01
B01
A01
C02
B02
A02
C03
B03
A03

I can do the first chunk using this code:

rev(TrapID[seq(1, 3)])

I've tried several versions of this code including doing a for loop where I ran each sequence through, but it never worked. Any thoughts as to how I can do the reversal of a sequence within my vector???

Allan

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-ecology mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Capture data

Drew Tyre
Allan

If TrapID occurs in both datasets you can put them together regardless of order using base::merge() or dplyr::left_join().

-- 
Drew Tyre

School of Natural Resources
University of Nebraska-Lincoln
416 Hardin Hall, East Campus
3310 Holdrege Street
Lincoln, NE 68583-0974

phone: +1 402 472 4054 
fax: +1 402 472 2946
email: [hidden email]
http://snr.unl.edu/tyre
http://atyre2.github.io
ORCID: orcid.org/0000-0001-9736-641X


> -----Original Message-----
> From: R-sig-ecology [mailto:[hidden email]] On Behalf
> Of Allan Edelsparre
> Sent: Wednesday, February 08, 2017 2:43 PM
> To: [hidden email]
> Subject: [R-sig-eco] Capture data
>
> Hi all,
>
> I have two data sets I want to merge. One has a vector with trap IDs and the
> vector from the other data set has the number of individuals captured at
> each trap site. I need to reverse the trap ID's within the trap ID vector in
> order to match the number trapped. Here is an example of how the vector
> looks like:
>
> TrapID
> A01
> B01
> C01
> A02
> B02
> C02
> A03
> B03
> C03
>
> I need the vector to look like this
>
> C01
> B01
> A01
> C02
> B02
> A02
> C03
> B03
> A03
>
> I can do the first chunk using this code:
>
> rev(TrapID[seq(1, 3)])
>
> I've tried several versions of this code including doing a for loop where I ran
> each sequence through, but it never worked. Any thoughts as to how I can
> do the reversal of a sequence within my vector???
>
> Allan
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology

_______________________________________________
R-sig-ecology mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Capture data

Don McKenzie
In reply to this post by Allan Edelsparre-2
turn TrapID into a matrix, with rows and columns = the numbers of letter codes and number codes respectively.
Use apply() to reverse the columns
turn it back into a vector

Three lines of code

I’ll leave it to you to figure the details.

> On Feb 8, 2017, at 12:43 PM, Allan Edelsparre <[hidden email]> wrote:
>
> Hi all,
>
> I have two data sets I want to merge. One has a vector with trap IDs and the vector from the other data set has the number of individuals captured at each trap site. I need to reverse the trap ID's within the trap ID vector in order to match the number trapped. Here is an example of how the vector looks like:
>
> TrapID
> A01
> B01
> C01
> A02
> B02
> C02
> A03
> B03
> C03
>
> I need the vector to look like this
>
> C01
> B01
> A01
> C02
> B02
> A02
> C03
> B03
> A03
>
> I can do the first chunk using this code:
>
> rev(TrapID[seq(1, 3)])
>
> I've tried several versions of this code including doing a for loop where I ran each sequence through, but it never worked. Any thoughts as to how I can do the reversal of a sequence within my vector???
>
> Allan
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology



_______________________________________________
R-sig-ecology mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Capture data

Glen Sargeant
In reply to this post by Allan Edelsparre-2
Here you go.

> a <- 1:3
> b <- 3:1
>
> idx <- as.matrix(expand.grid(b,a)[,c(2,1)])
>
> x <- c("A01", "B01", "C01", "A02", "B02", "C02", "A03", "B03", "C03")
>
> x[order(idx[,1],idx[,2])]
[1] "C01" "B01" "A01" "C02" "B02" "A02" "C03" "B03" "A03"


On Wed, Feb 8, 2017 at 2:43 PM, Allan Edelsparre <[hidden email]>
wrote:

> Hi all,
>
> I have two data sets I want to merge. One has a vector with trap IDs and
> the vector from the other data set has the number of individuals captured
> at each trap site. I need to reverse the trap ID's within the trap ID
> vector in order to match the number trapped. Here is an example of how the
> vector looks like:
>
> TrapID
> A01
> B01
> C01
> A02
> B02
> C02
> A03
> B03
> C03
>
> I need the vector to look like this
>
> C01
> B01
> A01
> C02
> B02
> A02
> C03
> B03
> A03
>
> I can do the first chunk using this code:
>
> rev(TrapID[seq(1, 3)])
>
> I've tried several versions of this code including doing a for loop where
> I ran each sequence through, but it never worked. Any thoughts as to how I
> can do the reversal of a sequence within my vector???
>
> Allan
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>
>


--
Glen Sargeant, Ph.D.
Research Wildlife Biologist/Statistician
USGS Northern Prairie Wildlife Research Center
E-mail: [hidden email]
Phone: (701) 253-5528

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-ecology mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Glen Sargeant
Research Wildlife Biologist
Loading...