The Art of R Programming

(WallPaper) #1
$means
[1] 3

$that
[1] 4 40
...

Here’s code to present the list in alphabetical order by word:

1 # sorts wrdlst, the output of findwords() alphabetically by word
2 alphawl <- function(wrdlst) {
3 nms <- names(wrdlst) # the words
4 sn <- sort(nms) # same words in alpha order
5 return(wrdlst[sn]) # return rearranged version
6 }

Since our words are the names of the list components, we can extract
the words by simply callingnames(). We sort these alphabetically, and then in
line 5, we use the sorted version as input to list indexing, giving us a sorted
version of the list. Note the use of single brackets, rather than double, as
the former are required for subsetting lists. (You might also consider using
order()instead ofsort(), which we’ll look at in Section 8.3.)
Let’s try it out.

> alphawl(wl)
$and
[1] 25

$be
[1] 67

$but
[1] 32

$case
[1] 17

$consists
[1] 20 41

$example
[1] 50
...

It works fine. The entry forandwas displayed first, thenbe, and so on.

96 Chapter 4

Free download pdf