Here are the worked example in-session activities associated with the lecture here
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.
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.
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.
me <- c(first_name, middle_name, last_name)
me
[1] "John" "Jacob" "Jingleheimer-Schmidt"
These variables are put into a vector.
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"
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