Re: [R] Subsetting vector with preserved order

2014-01-02 Thread Hervé Pagès
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

Re: [R] Subsetting vector with preserved order

2014-01-02 Thread arun
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