## ignore this fiddly bit and just leave it as is knitr::opts_chunk$set(comment=NA, error=T, cache=T) library("litdata")
The following lines, which use str_detect
, a function we haven't yet studied, derive a character vector with one element for each chapter of The Sheik.
# strip off all but the body text body_ll <- sheik_ll[match("CHAPTER I", sheik_ll): (match("THE END", sheik_ll) - 1)] # find chapter heading indices: # str_detect gives a logical vector telling us which lines # match the pattern; which tells us the indices chap_indices <- which(str_detect(body_ll, "^CHAPTER [IVX]")) body_lc <- tolower(body_ll) # initialize results vector sheik_chaps <- character(length(chap_indices)) # tricky edge condition: contrive the last "chapter start" # to be one past the last line chap_indices <- c(chap_indices, length(body_ll) + 1) for (j in seq_along(chap_indices[-length(chap_indices)])) { sheik_chaps[j] <- str_c(body_lc[chap_indices[j]: (chap_indices[j + 1] - 1)], collapse=" ") }
Now we split this up using our friend str_split
:
sheik_chaps_words <- str_split(sheik_chaps, "\\W+")
sheik_chaps_words
is a list.
Now write a for
loop that counts the fraction of times "Arab" and "Diana" occur in each chapter, storing the results in two vectors.
arab <- numeric() diana <- numeric() # your for loop here
These lines will print out your results as occurrences per 10000 words:
For arab
:
round(arab * 10000)
For diana
:
round(diana * 10000)
Write down expressions that yield the following information by subscripting laureates
.
# your expression here
# your expression here
# your expression here
# your expression here
x
and its ordering permutation order(x)
.YOUR EXPLANATION HERE
laur_small <- laureates[1:5, c("surname", "bornCountry", "gender", "year")]
Write an expression to sort this table alphabetically by surname.
# your expression for sorting laur_small here
# your expression for sorting laur_small here
laur_small
by gender and year of prize. You'll need two $
expressions.# your expression for sorting laur_small here
laureates
to show first names, surnames, birth dates, and prize years of the five most recently-born laureates. # your expression here
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.