---
title: "Splitting and Combining Strings"
author: "Statistical Computing, 36-350"
date: "Friday September 2, 2016"
---

Splitting a string
===

To split based on a keyword, use the `strsplit()` function 

```{r}
ingredients = "chickpeas, tahini, olive oil, garlic, salt"
strsplit(ingredients, split=",")
```

Looks like a weird output format, what is it?

```{r}
ingredients = "chickpeas, tahini, olive oil, garlic, salt"
split.obj = strsplit(ingredients, split=",")
class(split.obj)
length(split.obj)
```

A list! With just one element, and that element is a vector of strings

`strsplit()` vectorizes
===

Just like `substring()`, `nchar()`, and the many others

```{r}
great.profs = "Nugent, Genovese, Greenhouse, Seltman, Shalizi, Ventura"
favorite.cats = "tiger, leopard, jaguar, lion"
split.list = strsplit(c(ingredients, great.profs, favorite.cats), split=",")
split.list
```

Returned object is a list with 3 elements. Each one a vector of strings, having lengths 5, 6, and 4 (now you see why we need a list?)

Reminder: list access tricks
===

Those darned lists can be tricky beasts, but super useful once you master them

```{r}
split.list[[1]] # This is a vector
split.list[1] # This is actually list of length 1
split.list[2:3] # Can't do this with [[ ]]
split.list[-(2:3)] # Also can't do this with [[ ]]
split.list[[2]] = NULL 
split.list # The 2nd element (vector of Professor names) was deleted
```

Splitting character-by-character
===

Finest splitting you can do is character-by-character; for this, use `strsplit()` with `split=""`

```{r}
split.chars = strsplit(ingredients, split="")[[1]]
split.chars
length(split.chars)
nchar(ingredients) # Matches the previous count
```

Combining strings
===

To join two (or more) strings into one, separated by a keyword, use the `paste()` function

```{r}
paste("Spider", "Man") # Default is to separate by " "
paste("Spider", "Man", sep="-")
paste("Spider", "Man", "does whatever", sep=", ")
```

`paste()` vectorizes
===

Just like `strsplit()`, `substring()`, `nchar()`, etc. Seeing a theme yet?

```{r}
presidents = c("Clinton", "Bush", "Reagan", "Carter", "Ford")
paste(presidents, c("D", "R", "R", "D", "R"))
paste(presidents, c("D", "R")) # Notice the recycling (not historically accurate!)
paste(presidents, " (", 42:38, ")", sep="")
```

Condensing a vector of strings
===

Can condense a vector of strings into one big string by using `paste()` with the `collapse` argument

```{r}
presidents
paste(presidents, collapse="; ")
paste(presidents, " (", 42:38, ")", sep="", collapse="; ")
paste(presidents, " (", c("D", "R", "R", "D", "R"), 42:38, ")", sep="", collapse="; ")
paste(presidents, collapse=NULL) # No condensing
```

Another example 
===

Can think of `strsplit()` and `paste()` with `collapse` as inverses of each other

```{r}
ingredients
split.ing = strsplit(ingredients, split=",")[[1]]
split.ing
paste(split.ing, collapse=",")
```

Another example
===

Here we make use of some list tricks and trimming tricks

```{r}
c(ingredients, great.profs, favorite.cats)
split.list = strsplit(c(ingredients, great.profs, favorite.cats), split=",")
split.list
split.vec = unlist(split.list)
split.vec
split.scrambled = sample(split.vec)
split.scrambled
split.scrambled.trimmed = trimws(split.scrambled) 
split.scrambled.trimmed # Trimmed whitespaces for us!
paste(split.scrambled.trimmed, collapse=" + ")
```