I have this toy data as df
:
structure(list(Product_Name = c("Delicious Chips", "Creamy Tomato Soup",
"Cheesy Macaroni", "Savory Meatballs", "Crispy Chicken Tenders"
), Ingredients = c("Potato Slices | Vegetable Oil | Salt | Seasoning Blend",
"Tomatoes | Water | Cream | Onions | Salt | Spices", "Macaroni | Cheese Sauce | Milk | Butter | Salt | Pepper",
"Ground Meat | Breadcrumbs | Onions | Garlic | Spices", "Chicken Tenders | Breading Mix | Vegetable Oil | Salt | Pepper"
)), row.names = c(NA, 5L), class = "data.frame")
Here I want to find which rows contain "Salt"
in the Ingredients
variable.
Using library(tidyverse)
, initially I try df %>% str_detect(Ingredients, "Salt")
but I get Error: object 'Ingredients' not found
.
But when I change it to df %>% filter(str_detect(Ingredients, "Salt")
it returns a dataframe with the products matching the string.
I thought str_detect
needs a character vector or something coercible to one and I thought that Ingredients
fit that because when I do class(df$Ingredients)
it returns character. Why won't it take Ingredients
as an argument and what changes when it is wrapped into filter()
?