考虑以下名为estimates_df
的数据帧.
....
Item section
7596 5 Gal Samandoque Cacti/Accents
7597 5 Gal Purple Prickly Pear Cacti/Accents
7598 5 Gal Banana Yucca Cacti/Accents
7599 5 Gal Yucca Vine Vines
7600 5 Gal Red Three Awn Grasses
7601 3/4" Screened To Match Existing Decomposed Granite
...
我还有一个名为cactus_names
的仙人掌/肉质名称的字符向量.
[1]"Prickly Pear"
[2]"Samandoque"
[3]"Banana Yucca"
...
我不想更改Item
列中的全名,但我想根据我的仙人掌/肉质载体中出现的名称更改section
列.我很难做到这一点,因为向量中的名称与列中的名称不完全匹配.例如,我try 过这样做:
estimates_df %>%
mutate(section = ifelse(cactus_names %in% Item, "Cacti/Succulents", section)
显然,这与任何名称都不匹配,因为它们不完全匹配.我希望最终结果是这样的:
....
Item section
7596 5 Gal Samandoque Cacti/Succulents
7597 5 Gal Purple Prickly Pear Cacti/Succulents
7598 5 Gal Banana Yucca Cacti/Succulents
7599 5 Gal Yucca Vine Vines
7600 5 Gal Red Three Awn Grasses
7601 3/4" Screened To Match Existing Decomposed Granite
...