Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. # Lookup List
  2. fruits <- c("guava","avocado", "apricots","kiwifruit","banana")
  3. vegies <- c("tomatoes", "asparagus", "peppers", "broccoli", "leafy greens")
  4.  
  5. # Patterns
  6. foods <- c("guava", "banana", "broccoli")
  7. patterns <- str_c(foods, collapse="|")
  8.  
  9. # Sample Sentence
  10. sentence <- "I want to eat banana and broccoli!"
  11.  
  12.  
  13. typeOfFood <- function(foods) {
  14.  
  15. if( foods %in% fruits ){
  16. type <- "FRUITS"
  17. }
  18. else if( foods %in% vegies ){
  19. type <- "VEGIES"
  20. }
  21. paste0(foods,"(",type,")")
  22.  
  23. }
  24.  
  25. str_replace_all(sentence, patterns, typeOfFood)
  26.  
  27. [1] "I want to eat banana(FRUITS) and broccoli(VEGIES)!"
  28.  
  29. sentence <- "I want to eat BANANA and Broccoli!"
  30.  
  31. [1] "I want to eat BANANA(FRUITS) and Broccoli(VEGIES)!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement