R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

## Each of the 12 chapters of Alice in Wonderland must be placed in a separate excel file and then all files into one folder. 
## My folder is titled "Alice in Wonderland"

library(tm)
## Loading required package: NLP
docs <- Corpus(DirSource("/Users/drlakeshialegettejones/Library/Mobile Documents/com~apple~CloudDocs/Desktop/ALL FILES!/Documents/All Class Info/Data Mining with R/Alice in Wonderland"))
docs
## <<SimpleCorpus>>
## Metadata:  corpus specific: 1, document level (indexed): 0
## Content:  documents: 12
## The remainder of the code follows directly from our textbook pages 75-77
#docs[[2]]
content(docs[[2]])[1:3]
## [1] "\"“Curiouser and curiouser!” cried Alice (she was so much surprised, that for the moment she quite forgot how to speak good English); “now I’m opening out like the largest telescope that ever was! Good-bye, feet!” (for when she looked down at her feet, they seemed to be almost out of sight, they were getting so far off). “Oh, my poor little feet, I wonder who will put on your shoes and stockings for you now, dears? I’m sure I shan’t be able! I shall be a great deal too far off to trouble myself about you: you must manage the best way you can;—but I must be kind to them,” thought Alice, “or perhaps they won’t walk the way I want to go! Let me see: I’ll give them a new pair of boots every Christmas.”\"\n\"And she went on planning to herself how she would manage it. “They must go by the carrier,” she thought; “and how funny it’ll seem, sending presents to one’s own feet! And how odd the directions will look!\"\n\n\"     Alice’s Right Foot, Esq.,\"\n\"       Hearthrug,\"\n\"         near the Fender,\"\n           (with Alice’s love).\n\n\"Oh dear, what nonsense I’m talking!”\"\n\"Just then her head struck against the roof of the hall: in fact she was now more than nine feet high, and she at once took up the little golden key and hurried off to the garden door.\"\n\"Poor Alice! It was as much as she could do, lying down on one side, to look through into the garden with one eye; but to get through was more hopeless than ever: she sat down and began to cry again.\"\n\"“You ought to be ashamed of yourself,” said Alice, “a great girl like you,” (she might well say this), “to go on crying in this way! Stop this moment, I tell you!” But she went on all the same, shedding gallons of tears, until there was a large pool all round her, about four inches deep and reaching half down the hall.\"\n\"After a time she heard a little pattering of feet in the distance, and she hastily dried her eyes to see what was coming. It was the White Rabbit returning, splendidly dressed, with a pair of white kid gloves in one hand and a large fan in the other: he came trotting along in a great hurry, muttering to himself as he came, “Oh! the Duchess, the Duchess! Oh! won’t she be savage if I’ve kept her waiting!” Alice felt so desperate that she was ready to ask help of any one; so, when the Rabbit came near her, she began, in a low, timid voice, “If you please, sir—” The Rabbit started violently, dropped the white kid gloves and the fan, and skurried away into the darkness as hard as he could go.\"\n\"Alice took up the fan and gloves, and, as the hall was very hot, she kept fanning herself all the time she went on talking: “Dear, dear! How queer everything is to-day! And yesterday things went on just as usual. I wonder if I’ve been changed in the night? Let me think: was I the same when I got up this morning? I almost think I can remember feeling a little different. But if I’m not the same, the next question is, Who in the world am I? Ah, that’s the great puzzle!” And she began thinking over all the children she knew that were of the same age as herself, to see if she could have been changed for any of them.\"\n\"“I’m sure I’m not Ada,” she said, “for her hair goes in such long ringlets, and mine doesn’t go in ringlets at all; and I’m sure I can’t be Mabel, for I know all sorts of things, and she, oh! she knows such a very little! Besides, she’s she, and I’m I, and—oh dear, how puzzling it all is! I’ll try if I know all the things I used to know. Let me see: four times five is twelve, and four times six is thirteen, and four times seven is—oh dear! I shall never get to twenty at that rate! However, the Multiplication Table doesn’t signify: let’s try Geography. London is the capital of Paris, and Paris is the capital of Rome, and Rome—no, that’s all wrong, I’m certain! I must have been changed for Mabel! I’ll try and say ‘How doth the little—’” and she crossed her hands on her lap as if she were saying lessons, and began to repeat it, but her voice sounded hoarse and strange, and the words did not come the same as they used to do:—\"\n\n“How doth the little crocodile\n\"    Improve his shining tail,\"\nAnd pour the waters of the Nile\n    On every golden scale!\n\n\"“How cheerfully he seems to grin,\"\n\"    How neatly spread his claws,\"\nAnd welcome little fishes in\n    With gently smiling jaws!”\n\n\"“I’m sure those are not the right words,” said poor Alice, and her eyes filled with tears again as she went on, “I must be Mabel after all, and I shall have to go and live in that poky little house, and have next to no toys to play with, and oh! ever so many lessons to learn! No, I’ve made up my mind about it; if I’m Mabel, I’ll stay down here! It’ll be no use their putting their heads down and saying ‘Come up again, dear!’ I shall only look up and say ‘Who am I then? Tell me that first, and then, if I like being that person, I’ll come up: if not, I’ll stay down here till I’m somebody else’—but, oh dear!” cried Alice, with a sudden burst of tears, “I do wish they would put their heads down! I am so very tired of being all alone here!”\"\n\"As she said this she looked down at her hands, and was surprised to see that she had put on one of the Rabbit’s little white kid gloves while she was talking. “How can I have done that?” she thought. “I must be growing small again.” She got up and went to the table to measure herself by it, and found that, as nearly as she could guess, she was now about two feet high, and was going on shrinking rapidly: she soon found out that the cause of this was the fan she was holding, and she dropped it hastily, just in time to avoid shrinking away altogether.\"\n\"“That was a narrow escape!” said Alice, a good deal frightened at the sudden change, but very glad to find herself still in existence; “and now for the garden!” and she ran with all speed back to the little door: but, alas! the little door was shut again, and the little golden key was lying on the glass table as before, “and things are worse than ever,” thought the poor child, “for I never was so small as this before, never! And I declare it’s too bad, that it is!”\"\n\"As she said these words her foot slipped, and in another moment, splash! she was up to her chin in salt water. Her first idea was that she had somehow fallen into the sea, “and in that case I can go back by railway,” she said to herself. (Alice had been to the seaside once in her life, and had come to the general conclusion, that wherever you go to on the English coast you find a number of bathing machines in the sea, some children digging in the sand with wooden spades, then a row of lodging houses, and behind them a railway station.) However, she soon made out that she was in the pool of tears which she had wept when she was nine feet high.\"\n\"“I wish I hadn’t cried so much!” said Alice, as she swam about, trying to find her way out. “I shall be punished for it now, I suppose, by being drowned in my own tears! That will be a queer thing, to be sure! However, everything is queer to-day.”\"\n\"Just then she heard something splashing about in the pool a little way off, and she swam nearer to make out what it was: at first she thought it must be a walrus or hippopotamus, but then she remembered how small she was now, and she soon made out that it was only a mouse that had slipped in like herself.\"\n\"“Would it be of any use, now,” thought Alice, “to speak to this mouse? Everything is so out-of-the-way down here, that I should think very likely it can talk: at any rate, there’s no harm in trying.” So she began: “O Mouse, do you know the way out of this pool? I am very tired of swimming about here, O Mouse!” (Alice thought this must be the right way of speaking to a mouse: she had never done such a thing before, but she remembered having seen in her brother’s Latin Grammar, “A mouse—of a mouse—to a mouse—a mouse—O mouse!”) The Mouse looked at her rather inquisitively, and seemed to her to wink with one of its little eyes, but it said nothing.\"\n\"“Perhaps it doesn’t understand English,” thought Alice; “I daresay it’s a French mouse, come over with William the Conqueror.” (For, with all her knowledge of history, Alice had no very clear notion how long ago anything had happened.) So she began again: “Où est ma chatte?” which was the first sentence in her French lesson-book. The Mouse gave a sudden leap out of the water, and seemed to quiver all over with fright. “Oh, I beg your pardon!” cried Alice hastily, afraid that she had hurt the poor animal’s feelings. “I quite forgot you didn’t like cats.”\"\n\"“Not like cats!” cried the Mouse, in a shrill, passionate voice. “Would you like cats if you were me?”\"\n\"“Well, perhaps not,” said Alice in a soothing tone: “don’t be angry about it. And yet I wish I could show you our cat Dinah: I think you’d take a fancy to cats if you could only see her. She is such a dear quiet thing,” Alice went on, half to herself, as she swam lazily about in the pool, “and she sits purring so nicely by the fire, licking her paws and washing her face—and she is such a nice soft thing to nurse—and she’s such a capital one for catching mice—oh, I beg your pardon!” cried Alice again, for this time the Mouse was bristling all over, and she felt certain it must be really offended. “We won’t talk about her any more if you’d rather not.”\"\n\"“We indeed!” cried the Mouse, who was trembling down to the end of his tail. “As if I would talk on such a subject! Our family always hated cats: nasty, low, vulgar things! Don’t let me hear the name again!”\"\n\"“I won’t indeed!” said Alice, in a great hurry to change the subject of conversation. “Are you—are you fond—of—of dogs?” The Mouse did not answer, so Alice went on eagerly: “There is such a nice little dog near our house I should like to show you! A little bright-eyed terrier, you know, with oh, such long curly brown hair! And it’ll fetch things when you throw them, and it’ll sit up and beg for its dinner, and all sorts of things—I can’t remember half of them—and it belongs to a farmer, you know, and he says it’s so useful, it’s worth a hundred pounds! He says it kills all the rats and—oh dear!” cried Alice in a sorrowful tone, “I’m afraid I’ve offended it again!” For the Mouse was swimming away from her as hard as it could go, and making quite a commotion in the pool as it went.\"\n\"So she called softly after it, “Mouse dear! Do come back again, and we won’t talk about cats or dogs either, if you don’t like them!” When the Mouse heard this, it turned round and swam slowly back to her: its face was quite pale (with passion, Alice thought), and it said in a low trembling voice, “Let us get to the shore, and then I’ll tell you my history, and you’ll understand why it is I hate cats and dogs.”\"\n\"It was high time to go, for the pool was getting quite crowded with the birds and animals that had fallen into it: there were a Duck and a Dodo, a Lory and an Eaglet, and several other curious creatures. Alice led the way, and the whole party swam to the shore.\""
## [2] NA                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
## [3] NA
#install.packages("magrittr")
library(magrittr)
library(tm)

docs <- docs %>%
        tm_map(removePunctuation) %>%
        tm_map(removeNumbers) %>%
        tm_map(removeWords, stopwords("en")) %>%
        tm_map(stripWhitespace) %>%
        tm_map(stemDocument)
content(docs[[2]])[1:3]
## [1] "“Curious curiouser” cri Alice much surpris moment quit forgot speak good English “now I’m open like largest telescop ever Goodby feet” look feet seem almost sight get far “Oh poor littl feet I wonder will put shoe stock now dear I’m sure I shan’t abl I shall great deal far troubl must manag best way can— I must kind ” thought Alice “ perhap won’t walk way I want go Let see I’ll give new pair boot everi Christmas” And went plan manag “They must go carrier” thought “ funni ’ll seem send present one’ feet And odd direct will look Alice’ Right Foot Esq Hearthrug near Fender Alice’ love Oh dear nonsens I’m talking” Just head struck roof hall fact now nine feet high took littl golden key hurri garden door Poor Alice It much lie one side look garden one eye get hopeless ever sat began cri “You asham ” said Alice “ great girl like ” might well say “ go cri way Stop moment I tell ” But went shed gallon tear larg pool round four inch deep reach half hall After time heard littl patter feet distanc hastili dri eye see come It White Rabbit return splendid dress pair white kid glove one hand larg fan came trot along great hurri mutter came “Oh Duchess Duchess Oh won’t savag I’ve kept waiting” Alice felt desper readi ask help one Rabbit came near began low timid voic “If pleas sir—” The Rabbit start violent drop white kid glove fan skurri away dark hard go Alice took fan glove hall hot kept fan time went talk “Dear dear How queer everyth today And yesterday thing went just usual I wonder I’ve chang night Let think I I got morn I almost think I can rememb feel littl differ But I’m next question Who world I Ah ’s  great puzzle” And began think children knew age see chang “I’m sure I’m Ada” said “ hair goe long ringlet mine doesn’t go ringlet I’m sure I can’t Mabel I know sort thing oh know littl Besides ’s   I’m I —oh dear puzzl I’ll tri I know thing I use know Let see four time five twelv four time six thirteen four time seven —oh dear I shall never get twenti rate Howev Multipl Tabl doesn’t signifi let’ tri Geographi London capit Pari Pari capit Rome Rome— ’s  wrong I’m certain I must chang Mabel I’ll tri say ‘How doth little—’” cross hand lap say lesson began repeat voic sound hoars strang word come use — “How doth littl crocodil     Improve shine tail And pour water Nile     On everi golden scale “How cheer seem grin     How neat spread claw And welcom littl fish     With gentl smile jaws” “I’m sure right words” said poor Alice eye fill tear went “I must Mabel I shall go live poki littl hous next toy play oh ever mani lesson learn No I’ve made mind I’m Mabel I’ll stay It’ll use put head say ‘Come dear’ I shall look say ‘Who I Tell first I like person I’ll come I’ll stay till I’m somebodi else’— oh dear” cri Alice sudden burst tear “I wish   put head I   tire alon ” As said look hand surpris see put one Rabbit’ littl white kid glove talk “How can I done ” thought “I must grow small ” She got went tabl measur found near guess now two feet high go shrink rapid soon found caus fan hold drop hastili just time avoid shrink away altogeth “That   narrow escape” said Alice good deal frighten sudden chang glad find still exist “ now garden” ran speed back littl door ala littl door shut littl golden key lie glass tabl “ thing wors ever” thought poor child “ I never small never And I declar ’s bad ” As said word foot slip anoth moment splash chin salt water Her first idea somehow fallen sea “ case I can go back railway” said Alice seasid life come general conclus wherev go English coast find number bath machin sea children dig sand wooden spade row lodg hous behind railway station Howev soon made pool tear wept nine feet high “I wish I hadn’t cri much” said Alice swam tri find way “I shall punish now I suppos drown tear That will  queer thing sure Howev everyth queer today” Just heard someth splash pool littl way swam nearer make first thought must walrus hippopotamus rememb small now soon made mous slip like “Would use now” thought Alice “ speak mous Everyth outoftheway I think like can talk rate ’s harm trying” So began “O Mous know way pool I tire swim O Mouse” Alice thought must right way speak mous never done thing rememb seen brother’ Latin Grammar “A mouse— mouse— mouse— mouse—O mouse” The Mous look rather inquisit seem wink one littl eye said noth “Perhap doesn’t understand English” thought Alice “I daresay ’s French mous come William Conqueror” For knowledg histori Alice clear notion long ago anyth happen So began “Où est ma chatte” first sentenc French lessonbook The Mous gave sudden leap water seem quiver fright “Oh I beg pardon” cri Alice hastili afraid hurt poor animal’ feel “I quit forgot didn’t like cats” “Not like cats” cri Mous shrill passion voic “Would  lik cat ” “Well perhap ” said Alice sooth tone “don’t angri And yet I wish I show cat Dinah I think ’d take fanci cat see She dear quiet thing” Alice went half swam lazili pool “ sit pur nice fire lick paw wash face— nice soft thing nurse— ’s capit one catch mice—oh I beg pardon” cri Alice time Mous bristl felt certain must realli offend “We won’t talk ’d rather ” “We indeed” cri Mous trembl end tail “As  I  talk subject Our famili always hated cat nasti low vulgar thing Don’t let hear name ” “I won’t indeed” said Alice great hurri chang subject convers “Are — fond—— dogs” The Mous answer Alice went eager “There nice littl dog near hous I like show A littl brightey terrier know oh long cur brown hair And ’ll fetch thing throw ’ll sit beg dinner sort things—I can’t rememb half — belong farmer know say ’s use ’s worth hundr pound He say kill rat —oh dear” cri Alice sorrow tone “I’m afraid I’ve offend ” For Mous swim away hard go make quit commot pool went So call soft “Mous dear Do come back won’t talk cat dog either don’t like ” When Mous heard turn round swam slowli back face quit pale passion Alice thought said low trembl voic “Let us get shore I’ll tell histori ’ll understand I hate cat dogs” It high time go pool get quit crowd bird anim fallen Duck Dodo Lori Eaglet sever curious creatur Alice led way whole parti swam shore"
## [2] NA                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
## [3] NA
data <- DocumentTermMatrix(docs, control=list(weighting=weightTfIdf))
## Warning in TermDocumentMatrix.SimpleCorpus(x, control): custom functions are
## ignored
data
## <<DocumentTermMatrix (documents: 12, terms: 2853)>>
## Non-/sparse entries: 6279/27957
## Sparsity           : 82%
## Maximal term length: 46
## Weighting          : term frequency - inverse document frequency (normalized) (tf-idf)
inspect(data[1:2,1:5])
## <<DocumentTermMatrix (documents: 2, terms: 5)>>
## Non-/sparse entries: 6/4
## Sparsity           : 40%
## Maximal term length: 13
## Weighting          : term frequency - inverse document frequency (normalized) (tf-idf)
## Sample             :
##                Terms
## Docs                 across actually took       advic       advis       afraid
##   Chapter a.csv 0.003898635   0.003494115 0.003494115 0.002519457 0.0005701389
##   Chapter b.csv 0.000000000   0.000000000 0.000000000 0.000000000 0.0010985211
findFreqTerms(data,0.1)
## character(0)
findAssocs(data,"alice",0.3)
## $alice
## numeric(0)
newData <- removeSparseTerms(data,0.1)
newData
## <<DocumentTermMatrix (documents: 12, terms: 27)>>
## Non-/sparse entries: 297/27
## Sparsity           : 8%
## Maximal term length: 6
## Weighting          : term frequency - inverse document frequency (normalized) (tf-idf)
library(tibble)
as_tibble(as.matrix(newData))
## # A tibble: 12 × 27
##          ask      back      came   great    just    larg    long   might   minut
##        <dbl>     <dbl>     <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 0.000367  0.000245  0.000367  2.45e-4 3.67e-4 3.67e-4 4.89e-4 4.89e-4 1.22e-4
##  2 0.000118  0.000471  0.000354  5.89e-4 4.71e-4 2.36e-4 3.54e-4 1.18e-4 0      
##  3 0.000419  0.000419  0         1.40e-4 0       2.80e-4 6.99e-4 2.80e-4 1.40e-4
##  4 0         0.000192  0.000767  6.71e-4 4.79e-4 4.79e-4 3.84e-4 1.92e-4 3.84e-4
##  5 0.000217  0.000434  0.000326  0       5.43e-4 1.09e-4 1.09e-4 3.26e-4 8.69e-4
##  6 0.0000935 0.000187  0.000374  2.81e-4 4.68e-4 7.48e-4 1.87e-4 3.74e-4 2.81e-4
##  7 0.000773  0.0000966 0.0000966 5.80e-4 7.73e-4 1.93e-4 3.87e-4 3.87e-4 2.90e-4
##  8 0.0000981 0.000686  0.000883  2.94e-4 4.90e-4 2.94e-4 0       2.94e-4 5.88e-4
##  9 0.000604  0.000302  0.000201  2.01e-4 1.01e-4 2.01e-4 1.01e-4 3.02e-4 1.01e-4
## 10 0.000110  0.000440  0.000440  2.20e-4 2.20e-4 0       3.30e-4 1.10e-4 1.10e-4
## 11 0.000257  0         0.000129  6.43e-4 6.43e-4 3.86e-4 1.29e-4 0       1.29e-4
## 12 0.000232  0.000464  0.000232  3.48e-4 5.80e-4 2.32e-4 4.64e-4 1.16e-4 2.32e-4
## # ℹ 18 more variables: noth <dbl>, now <dbl>, pleas <dbl>, quit <dbl>,
## #   rather <dbl>, say <dbl>, seem <dbl>, sort <dbl>, talk <dbl>, tell <dbl>,
## #   think <dbl>, tri <dbl>, two <dbl>, word <dbl>, `’re` <dbl>, `“well` <dbl>,
## #   `“what` <dbl>, `can’t` <dbl>