0

I have a vector of words

c('Apple','Orange','Apple','Carrot','Onion','Onion') 

I want to categorize them in

list('fruit' = c('Apple', 'Orange'),
'vegetable' =  c('Carrot','Onion')

The output I am looking for is

c('fruit', 'fruit', 'fruit', 'vegetable', 'vegetable', 'vegetable') .

My current approach is to convert each of them to data.table and use merge to get the categories. Is there any other simpler solution?

imsc
  • 7,492
  • 7
  • 47
  • 69
  • 1
    In your simple case you could stick with the vectors. I would try `l <- c('Apple', 'Orange', 'Carrot','Onion') ; m <- rep(c("fruit", "vegetable"), each = 2) ; m[match(x, l)]`. I think we have plenty of dupes for this one. – David Arenburg Jun 30 '16 at 21:23
  • 1
    The answer by @Marek in the linked dupe should be fine here since you already have a lookup list. And it's certainly clean ("This is by far the easiest way", J. Ulrich) – Henrik Jun 30 '16 at 21:45
  • ["Character matching provides a powerful way to make lookup tables"](http://adv-r.had.co.nz/Subsetting.html#applications) (appears also in the linked dupe); perhaps even simpler, _if_ your lookup table was structured differently. – Henrik Jun 30 '16 at 21:51
  • Thanks for the links. Indeed the second link gives the simplest possible solution. – imsc Jun 30 '16 at 22:08

1 Answers1

1

Here's one alternative

x <- c('Apple','Orange','Apple','Carrot','Onion','Onion') 
lst <- list('fruit' = c('Apple', 'Orange'),
'vegetable' =  c('Carrot','Onion'))
with(stack(lst), ind[match(x, values)])
# [1] fruit     fruit     fruit     vegetable vegetable vegetable
lukeA
  • 53,097
  • 5
  • 97
  • 100