I'm working on a large dataset where I need to create a new column and assign appropriate labels. So for instance, I have rows containing a mixture of fruits and vegetables, and I want a new column where it identifies the row as fruit, vegetable, or mixed. An ideal outcome is:
# name type
# 1 apple fruit
# 2 bananas fruit
# 3 kale vegetable
# 4 apple, kale mixed
While I'm successful at identifying types for rows containing single "items", I'm having trouble coding for "mixed" type. I was thinking of using AND (for example, mixed = fruit AND vegetable).. but this didn't work. Is there an efficient/correct way to coding for mixed instances? I appreciate your help, thank you!!
So right now what I have is:
df <- data.frame(name = c('apple','bananas','kale','apple, kale'),
stringsAsFactors = FALSE)
df$type <- factor(df$name) # First step: copy vector and make it factor
#Change levels:
levels(df$type) <- list(
fruit = c("apple", "bananas"),
vegetable = c("kale")
)