Module 11 Strings

ex1 <- c("Ashland","Bayfield","Douglas","Adams","Buffalo","Jefferson")
ex2 <- c("Chinook Salmon","Coho Salmon","Chum Salmon",
         "Pink Salmon","Sockeye Salmon","Salmon")
ex3 <- c("WD40","JB80","R2D2","C3PO","Run DMC","AC/DC")

str_view(ex1,"l")
str_view(ex1,"la")
str_view(ex1,".l.")
str_view(ex1,".la.")
str_view(ex1,"^A")
str_view(ex1,"^A.")
str_view(ex1,"s$")
str_view(ex1,".s$")
str_view(ex2,"Salmon")
str_view(ex2,"^Salmon$")
str_view(ex3,"\\d")
str_view(ex3,"\\s")
str_view(ex1,"[sl]")
str_view(ex1,"[^sl]")
str_view(ex1,"a|l")
str_view_all(ex1,"a|l")
str_view(ex1,"[AEIOU]")
str_view(ex1,"[^AEIOU]")
str_view(ex1,"[aeiou]")
str_view(ex1,"ff?")
str_view(ex1,"ff+")
str_view(ex1,"ff*")
str_view(ex1,"ff{1}")
str_view(ex1,"l")
str_detect(ex1,"l")
#R>  [1]  TRUE  TRUE  TRUE FALSE  TRUE FALSE
ex1[str_detect(ex1,"l")]
#R>  [1] "Ashland"  "Bayfield" "Douglas"  "Buffalo"
str_count(ex1,"l")
#R>  [1] 1 1 1 0 1 0
str_replace(ex1,"f","-")
#R>  [1] "Ashland"   "Bay-ield"  "Douglas"   "Adams"     "Bu-falo"   "Je-ferson"
str_replace_all(ex1,"f","-")
#R>  [1] "Ashland"   "Bay-ield"  "Douglas"   "Adams"     "Bu--alo"   "Je--erson"
str_replace(ex1,"[a|f]","-")
#R>  [1] "Ashl-nd"   "B-yfield"  "Dougl-s"   "Ad-ms"     "Bu-falo"   "Je-ferson"
str_replace_all(ex1,"[a|f]","-")
#R>  [1] "Ashl-nd"   "B-y-ield"  "Dougl-s"   "Ad-ms"     "Bu---lo"   "Je--erson"
str_replace_all(ex1,c("a"="-","f"="*"))
#R>  [1] "Ashl-nd"   "B-y*ield"  "Dougl-s"   "Ad-ms"     "Bu**-lo"   "Je**erson"
str_split(ex2,"\\s")
#R>  [[1]]
#R>  [1] "Chinook" "Salmon" 
#R>  
#R>  [[2]]
#R>  [1] "Coho"   "Salmon"
#R>  
#R>  [[3]]
#R>  [1] "Chum"   "Salmon"
#R>  
#R>  [[4]]
#R>  [1] "Pink"   "Salmon"
#R>  
#R>  [[5]]
#R>  [1] "Sockeye" "Salmon" 
#R>  
#R>  [[6]]
#R>  [1] "Salmon"
str_split(ex2,"\\s",simplify=TRUE)
#R>       [,1]      [,2]    
#R>  [1,] "Chinook" "Salmon"
#R>  [2,] "Coho"    "Salmon"
#R>  [3,] "Chum"    "Salmon"
#R>  [4,] "Pink"    "Salmon"
#R>  [5,] "Sockeye" "Salmon"
#R>  [6,] "Salmon"  ""
str_split(ex2,"\\s",simplify=TRUE,n=1)
#R>       [,1]            
#R>  [1,] "Chinook Salmon"
#R>  [2,] "Coho Salmon"   
#R>  [3,] "Chum Salmon"   
#R>  [4,] "Pink Salmon"   
#R>  [5,] "Sockeye Salmon"
#R>  [6,] "Salmon"
str_view_all(ex2,boundary("word"))
str_view_all(ex2,boundary("character"))
str_view_all(ex2,boundary("line"))
el <- readxl::read_excel(file.path("data","EcoLeague.xlsx"))
el
#R>  # A tibble: 6 x 3
#R>    Institution               Address               City_State_ZIP      
#R>    <chr>                     <chr>                 <chr>               
#R>  1 Alaska Pacific University 4101 University Drive Anchorage, AK 99508 
#R>  2 College of the Atlantic   105 Eden Street       Bar Harbor, ME 04609
#R>  3 Dickinson College         P.O. Box 1773         Carlisle, PA 17013  
#R>  4 New College of Florida    5800 Bay Shore Road   Sarasota, FL 34243  
#R>  5 Northland College         1411 Ellis Avenue     Ashland, WI 54806   
#R>  6 Prescott College          220 Grove Avenue      Prescott, AZ 86301
str_view(el$Institution,"a")
str_view_all(el$Institution,"a")
str_view(el$Institution,".College.")
str_view(el$Institution,"College.")
str_view(el$Institution,".College")
str_view(el$Institution,".College.|.College|College.")
sum(str_detect(el$Institution,".College.|.College|College."))
#R>  [1] 5
str_view(el$Institution,"[col]")
str_view_all(el$Institution,"[col]")
str_view_all(el$Institution,boundary("word"))
str_extract_all(el$Institution,boundary("word"),simplify=TRUE)
#R>       [,1]        [,2]      [,3]         [,4]      
#R>  [1,] "Alaska"    "Pacific" "University" ""        
#R>  [2,] "College"   "of"      "the"        "Atlantic"
#R>  [3,] "Dickinson" "College" ""           ""        
#R>  [4,] "New"       "College" "of"         "Florida" 
#R>  [5,] "Northland" "College" ""           ""        
#R>  [6,] "Prescott"  "College" ""           ""
el %>% separate(City_State_ZIP,into=c("City","State_ZIP"),sep=", ") %>%
  separate(State_ZIP,into=c("State","ZIP"))
#R>  # A tibble: 6 x 5
#R>    Institution               Address               City       State ZIP  
#R>    <chr>                     <chr>                 <chr>      <chr> <chr>
#R>  1 Alaska Pacific University 4101 University Drive Anchorage  AK    99508
#R>  2 College of the Atlantic   105 Eden Street       Bar Harbor ME    04609
#R>  3 Dickinson College         P.O. Box 1773         Carlisle   PA    17013
#R>  4 New College of Florida    5800 Bay Shore Road   Sarasota   FL    34243
#R>  5 Northland College         1411 Ellis Avenue     Ashland    WI    54806
#R>  6 Prescott College          220 Grove Avenue      Prescott   AZ    86301