0

I have the following

Data <- data.frame(
  randnum = sample(1:10),
  fruit   = sample(c("apple", "pear", "plum"), 10, replace = TRUE))

I want to create a new column called Data$fruit.ID based on fruit with the corresponding ID stored elsewhere, in this case another dataframe (but only because I don't know any better!):

fruit.ID.data <- data.frame(
  fruit    = c("apple", "pear", "plum"),
  fruit.ID = c(123, 456, 789))

> fruit.ID.data
      fruit fruit.ID
1     apple      123
2      pear      456
3 starfruit      789

There are many fruits and many fruit.ID's so huge if.else, else, else is not a preferable option.

How can I pass the fruit ID info the to the if statement?

data$fruit.ID

Jim
  • 558
  • 4
  • 13
  • Please see the marked duplicate -- it looks like you are asking how to join two data frames on the fruit name. This would enable you to add the fruit id column to `Data` and proceed however you want with that id information. – josliber Feb 25 '20 at 14:18
  • perhaps, I will check it out thank you. – Jim Feb 25 '20 at 14:19
  • @Jim. I cannot understand your question at all. Can you try again? Your reproducible example is far too minimal. Which data frame is "huge": `Data` or `Fruit.ID.data`? – Edward Feb 25 '20 at 15:04
  • Hi Edward, really! I am sorry, I should have perhaps provided an desired output. Although the problem has been resolved using the link – Jim Feb 25 '20 at 15:12

0 Answers0