# Subsetting vector with preserved order

 Classic List Threaded
4 messages
Reply | Threaded
Open this post in threaded view
|

## Subsetting vector with preserved order

 This post has NOT been accepted by the mailing list yet. I have three vectors as follows: > a <- c('A','B','C','D','E') > b <- c('Tom','Dick','Harry','Bob','Joe') > d <- c('E','D','B') Subsetting b by using d on a, with b[a %in% d], gives the names in the order they appear in b: >  b[a %in% d] > [1] "Dick" "Bob"  "Joe" But I'd like them to show in the order in d, as "Joe" "Bob" "Dick". What is the easy way to do this? Thanks.
Reply | Threaded
Open this post in threaded view
|

## Re: Subsetting vector with preserved order

 Hi, Try ?match  b[match(d,a)] #[1] "Joe"  "Bob"  "Dick" A.K. I have three vectors as follows: > a <- c('A','B','C','D','E') > b <- c('Tom','Dick','Harry','Bob','Joe') > d <- c('E','D','B') Subsetting b by using d on a, with b[a %in% d], gives the names in the order they appear in b: >  b[a %in% d] > [1] "Dick" "Bob"  "Joe" But I'd like them to show in the order in d, as "Joe" "Bob" "Dick". What is the easy way to do this? Thanks. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Subsetting vector with preserved order

 Hi On 01/02/2014 04:04 PM, arun wrote: > Hi, > Try ?match >   b[match(d,a)] > #[1] "Joe"  "Bob"  "Dick" Or use 'a' to put names on 'b':    > names(b) <- a    > b          A       B       C       D       E      "Tom"  "Dick" "Harry"   "Bob"   "Joe" Then subset by names:    > b[d]         E      D      B     "Joe"  "Bob" "Dick" Cheers, H. > A.K. > > > I have three vectors as follows: > >> a <- c('A','B','C','D','E') >> b <- c('Tom','Dick','Harry','Bob','Joe') >> d <- c('E','D','B') > > Subsetting b by using d on a, with b[a %in% d], gives the names in the order they appear in b: > >>   b[a %in% d] >> [1] "Dick" "Bob"  "Joe" > > But I'd like them to show in the order in d, as "Joe" "Bob" "Dick". What is the easy way to do this? > > Thanks. > > ______________________________________________ > [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. > -- Hervé Pagès Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N, M1-B514 P.O. Box 19024 Seattle, WA 98109-1024 E-mail: [hidden email] Phone:  (206) 667-5791 Fax:    (206) 667-1319 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Subsetting vector with preserved order

 This post has NOT been accepted by the mailing list yet. In reply to this post by arun kirshna Great - thanks!