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

Name:  
Andrew ID:  
Collaborated with:  

This lab is to be completed in class. You can collaborate with your classmates, but you must identify their names above, and you must submit **your own** lab as an Rmd file on Blackboard, by 11:59pm on the day of the lab.

There are Homework 1 questions dispersed throughout. These must be written up in a separate Rmd document, together with all Homework 1 questions from other labs. Your homework writeup must start as this one: by listing your name, Andrew ID, and who you collaborated with. You must submit **your own** homework as a knit HTML file on Blackboard, by 6pm on **Sunday September 11**. This document contains 14 of the 45 total points for Homework 1.

**Important remark** on compiling the homework: many homework questions depend on variables that are defined in the surrounding lab. It is easiest just to copy and paste the contents of all these labs into **one big Rmd file**, with your lab solutions and homework solutions filled in, knit it, and submit the HTML file. So for Homework 1, e.g., you should paste your solutions (including solutions to homework questions) from Lab 1w, Lab 1f, Lab 2w, and Lab 2f into one big Rmd file, knit it, and submit it. 

Substrings, replacements
===

```{r}
bill = "Bill Clinton"
others = c("George Bush", "Ronald Reagan", "Jimmy Carter", "Gerald Ford")
```

- Display the first 5 letters from the string `bill`.

- Display the first 5 letters from each of the strings in the vector `others`.

- Join `bill` and `others` together into one vector of strings, called `presidents`.

- Display the last 3 letters from each of the strings in the vector `presidents`.

- Copy `presidents` over to a new string `presidents.borg`. Replace the last 4 letters in each of the strings in `presidents.borg` with "borg". Display the results.

**Hw1 Q1 (2 points).** Replace the last letter in each of the strings in `presidents` with the last letter of one of the other strings, randomly swapped. Display the results.

**Hw1 Q2 (5 points).** Explain the following replacement results.

```{r}
college = "Dietrich"
substr(college, 5, 9) = "right"
college

college = "Dietrich"
substr(college, 1, 3) = "Live"
college

words = c("fat", "hat", "lamp", "glade")
substr(words, 1, 1) = c("c", "b")
words

animals = c("cat", "lion", "piggy", "chicken")
substr(animals, 1, 5) = c("kitty", "doggy")
animals

subjects = c("Statistics", "Computer Science", "Mathematics", "Engineering")
ratings = c("Good", "Bad")
substr(subjects, nchar(subjects)-nchar(ratings)+1, nchar(subjects)) = ratings
subjects
```

Splitting and combining strings
===

```{r}
presidents = c("Bill Clinton", "George Bush", "Ronald Reagan", "Jimmy Carter", "Gerald Ford")
```

- Split each string in the vector `presidents` into two strings, according to first and last name. Save the resulting list as `presidents.list`, and display it (the result should be a list).

- Display names of only the democrats in `presidents.list`.

**Hw1 Q3 (2 points).** Run the command `presidents.mat = matrix(unlist(presidents.list), 5, 2, byrow=TRUE)`, and display and explain the result. What would have happened without the `byrow=TRUE`?

- Explain the splitting results below. 

```{r}
strsplit(presidents[4], split="o")
strsplit(presidents, split=c(" ", "o"))
```

- Create a vector of strings `vice.presidents`, containing the vice presidents corresponding to the presidents in `presidents` (look them up on the web if you need to...). Create another vector of strings `pairs`, by joining together each president with his vice president, separated by " & ". So, the first string in `pairs` should be "Bill Clinton & Al Gore". Display the results.

- Collapse the vector `pairs` in the previous question into a single string, where each pair of president and vice president is separated by ", ", so the string should read "Bill Clinton & Al Gore, George Bush & ...". 

- Do the same as in the previous question, but with the president and vice presidents listed in chronological order, i.e., starting with Ford and his vice president, and finishing with Clinton and Gore. (Hint: use `rev()`.)

**Hw1 Q4 (3 points).** Create a 5 x 2 matrix `vice.presidents.mat`, with the first column containing first names of vice presidents, and the second column containing last names. (Hint: follow Hw1 Q3 and the code leading up to this.) Now use `presidents.mat` and `vice.presidents.mat` to build a string that contains the last name of each president and his vice president pair, with the names separated by "+" and the pairs by ", ". So, the final string should be of the form "Clinton+Gore, Bush+...". (Hint: this last step can be done with a single `paste()` call.)

**Hw1 Q5 (2 points).** Do the same as in the previous question, but with each president assigned a random vice president. So, the final string should be of the form "Clinton+Quayle, Bush+...", where Clinton's vice president was chosen randomly from the 5 possibilities in `vice.presidents`. (Hint: scramble the second column of `vice.presidents.mat` before proceeding with the usual workflow.)