I have a dataframe with a variable that is composed of strings of product's names. Ex.:
| Names | Price ...|
|-----------------------------|----------|
| milk | ... |
| pizza with tomato | ... |
| tuna with olive and beans | ... |
| etc... | ... |
I would like to create an other variable that take as values names stored in a vector that match those in variable Names
Basically, if I have a vector:
x <- c("milk", "pizza", "tuna")
The code should look into the variable Names
and if it finds a match in vector x
then it takes that value and stores it in a new variable called tag
that should have as values milk
, pizza
, tuna
.
Is it possible to create a variable whose values are taken from another variable if conditions are met?
CodePudding user response:
If I am understanding correctly, you want to have a tag (like a keyword) in a new column in your dataframe. If so, then it is probably easier to just extract the word if it occurs in your search list using str_extract
.
library(stringr)
search <- c("milk", "pizza", "tuna")
df$tag <- stringr::str_extract(df$Names, paste(search, collapse="|"))
Output
Names price tag
1 milk 3.99 milk
2 pizza with tomato 5.99 pizza
3 tuna with olives and beans 9.99 tuna
4 crab 4.99 <NA>
Data
df <-
structure(list(
Names = c("milk", "pizza with tomato", "tuna with olives and beans",
"crab"),
price = c(3.99, 5.99, 9.99, 4.99),
tag = c("milk", "pizza",
"tuna", NA)
),
row.names = c(NA,-4L),
class = "data.frame")