Skip to content

using match-type function to return correctly ordered data from a dataframe

4 messages · Markus Weisner, Jeff Newmiller, William Dunlap

#
Have you actually read

?"%in%"

?

Although a valuable tool, not all answers are most effectively obtained by Googling.

Also, your repeated assertions that the answers are not maintained in order are poorly framed. They DO stay in order according to the zipcode database order. That said, your desire for numeric indexes is only as far away as your help file.
---------------------------------------------------------------------------
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.
Markus Weisner <r at themarkus.com> wrote:

            
#
Is the following what you want?

  > dfLETTERS <- data.frame(LETTER=LETTERS[1:5], lData=c("Ay","Bee","Cee","Dee","Eee"), row.names=sprintf("LRow%d",1:5))
  > z <- c("D", "B", "A", "B")
  > dfLETTERS[match(z, dfLETTERS$LETTER), ]
          LETTER lData
  LRow4        D   Dee
  LRow2        B   Bee
  LRow1        A    Ay
  LRow2.1      B   Bee
  > # or when z includes things not in the list to match:
  > dfLETTERS[match(c("E",NA,"notALetter","A"), dfLETTERS$LETTER), ]
        LETTER lData
  LRow5      E   Eee
  NA      <NA>  <NA>
  NA.1    <NA>  <NA>
  LRow1      A    Ay

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com