Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Lookup List
- fruits <- c("guava","avocado", "apricots","kiwifruit","banana")
- vegies <- c("tomatoes", "asparagus", "peppers", "broccoli", "leafy greens")
- # Patterns
- foods <- c("guava", "banana", "broccoli")
- patterns <- str_c(foods, collapse="|")
- # Sample Sentence
- sentence <- "I want to eat banana and broccoli!"
- typeOfFood <- function(foods) {
- if( foods %in% fruits ){
- type <- "FRUITS"
- }
- else if( foods %in% vegies ){
- type <- "VEGIES"
- }
- paste0(foods,"(",type,")")
- }
- str_replace_all(sentence, patterns, typeOfFood)
- [1] "I want to eat banana(FRUITS) and broccoli(VEGIES)!"
- sentence <- "I want to eat BANANA and Broccoli!"
- [1] "I want to eat BANANA(FRUITS) and Broccoli(VEGIES)!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement