mardi 25 février 2020

Passing a list of (many) conditions to an if statement in R [duplicate]

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

Aucun commentaire:

Enregistrer un commentaire