--- title: "String Basics" author: "Statistical Computing, 36-350" date: "Wednesday August 31, 2016" --- What are strings? === The simplest distinction: - **Character:** a symbol in a written language, like letters, numerals, punctuation, space, etc. - **String:** a sequence of characters bound together ```{r} class("r") class("Ryan") ``` Why do we care? === - A lot of interesting data out there is in character form! - Webpages, emails, surveys, logs, search queries, etc. - Even if you just care about numbers eventually, you'll need to understand how to get numbers from text How to make strings === Just use double quotes or single quotes and type anything in between ```{r} str.1 = "Statistical" str.2 = 'Computing' ``` We often prefer double quotes to single quotes, because then we can use apostrophes ```{r} str.3 = "isn't that bad" ``` Whitespaces === Whitespaces count as characters and can be included in strings: - `" "` for space - `"\n"` for newline - `"\t"` for tab ```{r} message = "Dear Mr. Carnegie,\n\nThanks for the great school!\n\nSincerely, Ryan" ``` Printing strings === To print to the console, use the `cat()` function ```{r} message cat(message) ``` Vectors of strings === The character is a basic data type in R (like numeric, or logical), so we can make vectors of out them. Just like we would with numbers ```{r} str.vec = c(str.1, str.2, str.3) # Collect 3 strings str.vec # All elements of the vector str.vec[3] # The 3rd element str.vec[-(1:2)] # All but the 1st and 2nd head(str.vec, 2) # The first 2 elements tail(str.vec, 2) # The last 2 elements rev(str.vec) # Reverse the order ``` Matrices of strings === Same idea with matrices ```{r} str.mat = matrix("", 2, 3) # Build an empty 2 x 3 matrix str.mat[1,] = str.vec # Fill the 1st row with str.vec str.mat[2,1:2] = str.vec[1:2] # Fill the 2nd row, only entries 1 and 2, with those of str.vec str.mat[2,3] = "isn't a fad" # Fill the 2nd row, 3rd entry, with a new string str.mat # All elements of the matrix t(str.mat) # Transpose of the matrix ``` Converting other data types to strings === Easy! Make things into strings with `as.character()` ```{r} as.character(0.8) as.character(0.8e+10) as.character(1:5) as.character(TRUE) ``` Converting strings to other data types === Not as easy! Depends on the given string, of course ```{r} as.numeric("0.5") as.numeric("0.5 ") as.numeric("0.5e-10") as.numeric("Hi!") as.logical("True") as.logical("TRU") as.numeric(c("0.5", "TRUE")) ``` Converting to lower or upper case === Use the `tolower()` or `toupper()` functions ```{r} tolower("I'M NOT ANGRY I SWEAR") toupper("Mom, I don't want my veggies") toupper("Hulk, sMasH") ```