Here are the worked example in-session activities associated with the lecture here

10 Minute Activity - Baby Steps with Strings

Create an R script file (File -> New File -> R Script) and save it as character_data.R in your project directory. After each of the steps below, Source (e.g., hit the source button) the file and look at the output in the console.

  1. Define a variables first_name, middle_name, last_name and fill them in with your own data. What happened when you Sourced the file?
first_name <- "John"
middle_name <- "Jacob"
last_name <- "Jingleheimer-Schmidt"

When you define a variable and source a file, it loads the variables into the environment.

 

  1. List the names of the variables, one on each line (AFTER!!!!!) their definition. What happened after the Source this time?
first_name
[1] "John"
middle_name
[1] "Jacob"
last_name
[1] "Jingleheimer-Schmidt"

When you source the file that lists the names of the variables, the values the variables are dumpted to the console.

 

  1. Put them into a variable named me that has them all together using the c() function (like we did for good old Ned) and on the next line put the name of this variable again. What happened after this Source?
me <- c(first_name, middle_name, last_name)
me
[1] "John"                 "Jacob"                "Jingleheimer-Schmidt"

These variables are put into a vector.

 

  1. Look up the function paste in the help files using the command ?paste. If you are given a few options, select the one with the title Concatenate Strings that is provided in the base package.
paste( me )
[1] "John"                 "Jacob"                "Jingleheimer-Schmidt"
paste( me, collapse = " ")
[1] "John Jacob Jingleheimer-Schmidt"
paste( me, sep = " ")
[1] "John"                 "Jacob"                "Jingleheimer-Schmidt"

15 Minute Activity - String Operations - Your Turn

Hey Jude Lyrics

url <- "https://raw.githubusercontent.com/dyerlab/ENVS-Lectures/master/data/hey_jude.txt"
text <- readLines( url )
text
[1] "Hey Jude, don't make it bad. Take a sad song and make it better. Remember to let her into your heart, Then you can start to make it better. Hey Jude, don't be afraid. You were made to go out and get her. The minute you let her under your skin, Then you begin to make it better. And anytime you feel the pain, hey Jude, refrain, Don't carry the world upon your shoulders. For well you know that it's a fool who plays it cool By making his world a little colder. Hey Jude, don't let me down. You have found her, now go and get her. Remember to let her into your heart, Then you can start to make it better. So let it out and let it in, hey Jude, begin, You're waiting for someone to perform with. And don't you know that it's just you, hey Jude, you'll do The movement you need is on your shoulder. Hey Jude, don't make it bad. Take a sad song and make it better. Remember to let her under your skin, Then you'll begin to make it Better better better better better better, oh. Na na na nananana, nannana, hey Jude... "

For this, I’m going to remove all the punctuation, make it all lower case and then tabulate the words after I split them into individual entries.

library(stringr)
text <- tolower(text) 
text <- gsub("[[:punct:]]","",text)
words <- str_split(text, pattern=" " )[[1]]
frequencies <- table( words )
rev(sort(frequencies))
words
      you        it    better        to      make      jude       hey       let       her       and 
       11        11        11         9         8         8         8         7         7         7 
     your      dont      then       the         a  remember        na     begin     youll     world 
        6         6         4         4         4         3         3         3         2         2 
    under      that      take     start      song      skin       sad       out      know       its 
        2         2         2         2         2         2         2         2         2         2 
     into     heart        go       get       for       can       bad     youre      with       who 
        2         2         2         2         2         2         2         1         1         1 
     were      well   waiting      upon   someone        so shoulders  shoulder   refrain     plays 
        1         1         1         1         1         1         1         1         1         1 
  perform      pain        on        oh       now      need   nannana  nananana  movement    minute 
        1         1         1         1         1         1         1         1         1         1 
       me    making      made    little      just        is        in       his      have     found 
        1         1         1         1         1         1         1         1         1         1 
     fool      feel      down        do      cool    colder     carry        by        be   anytime 
        1         1         1         1         1         1         1         1         1         1 
   afraid           
        1         1 
LS0tCnRpdGxlOiAiV29ya2VkIEFjdGl2aXRpZXMgLSBTZWN0aW9uIDIiCm91dHB1dDogaHRtbF9ub3RlYm9vawotLS0KCkhlcmUgYXJlIHRoZSB3b3JrZWQgZXhhbXBsZSBpbi1zZXNzaW9uIGFjdGl2aXRpZXMgYXNzb2NpYXRlZCB3aXRoIHRoZSBsZWN0dXJlIFtoZXJlXShzbGlkZXMuaHRtbCkKCgoKIyMgMTAgTWludXRlIEFjdGl2aXR5IC0gQmFieSBTdGVwcyB3aXRoIFN0cmluZ3MKCkNyZWF0ZSBhbiBSIHNjcmlwdCBmaWxlIChGaWxlIC0+IE5ldyBGaWxlIC0+IFIgU2NyaXB0KSBhbmQgc2F2ZSBpdCBhcyBjaGFyYWN0ZXJfZGF0YS5SIGluIHlvdXIgcHJvamVjdCBkaXJlY3RvcnkuIEFmdGVyIGVhY2gg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