[R] Is there a neat R trick for this?

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Tue Feb 12 22:05:35 CET 2013


I apologize for the noise. I did not read the original question carefully enough.

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204

> -----Original Message-----
> From: Jeff Newmiller [mailto:jdnewmil at dcn.davis.CA.us]
> Sent: Tuesday, February 12, 2013 11:37 AM
> To: Nordlund, Dan (DSHS/RDA); r-help at r-project.org
> Subject: Re: [R] Is there a neat R trick for this?
> 
> but that doesn't maintain the sequence of the original data.
> -----------------------------------------------------------------------
> ----
> Jeff Newmiller                        The     .....       .....  Go
> Live...
> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live
> Go...
>                                       Live:   OO#.. Dead: OO#..
> Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.
> rocks...1k
> -----------------------------------------------------------------------
> ----
> Sent from my phone. Please excuse my brevity.
> 
> "Nordlund, Dan (DSHS/RDA)" <NordlDJ at dshs.wa.gov> wrote:
> 
> >Another option is
> >
> >which(y %in% x)
> >
> >
> >Dan
> >
> >Daniel J. Nordlund
> >Washington State Department of Social and Health Services
> >Planning, Performance, and Accountability
> >Research and Data Analysis Division
> >Olympia, WA 98504-5204
> >
> >> -----Original Message-----
> >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> >> project.org] On Behalf Of Pascal Oettli
> >> Sent: Tuesday, February 12, 2013 2:21 AM
> >> To: Robert Latest
> >> Cc: r-help at r-project.org
> >> Subject: Re: [R] Is there a neat R trick for this?
> >>
> >> Hello,
> >>
> >> ?match
> >>
> >>  > x <- c(4,5,6)
> >>  > y <- c(10,1,5,12,4,13,14)
> >>  > match(x,y)
> >> [1]  5  3 NA
> >>
> >> Hope this helps,
> >> Pascal
> >>
> >>
> >> Le 12/02/2013 19:09, Robert Latest a écrit :
> >> > Hello all,
> >> >
> >> > given two vectors X and Y I'd like to receive a vector Z which
> >> > contains, for each element of X, the index of the corresponding
> >> > element in Y (or NA if that element isn't in Y).
> >> >
> >> > Example:
> >> >
> >> > x <- c(4,5,6)
> >> > y <- c(10,1,5,12,4,13,14)
> >> >
> >> > z <- findIndexIn(x, y)
> >> > z
> >> > [1] 5 3 NA
> >> >
> >> > 1st element of z is 5, because the 1st element of x is at the 5th
> >> position in y
> >> > 2nd element of z is 3, because the 2nd element of x is at the 3rd
> >> position in y
> >> > 3rd element of z is NA, because the 3rd element of x is not in y
> >> >
> >> > Of course I can write the function findIndexIn() using a for loop,
> >> but
> >> > in 80% of cases when I felt the urge to use "for" in R it turned
> >out
> >> > that there was already some builtin operator or function that did
> >the
> >> > trick.
> >> >
> >> > Suggestions, anyone?
> >> > Thanks,
> >> >
> >> > robert
> >> >
> >> > ______________________________________________
> >> > 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.
> >______________________________________________
> >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.



More information about the R-help mailing list