How to get frequency of word in a sentence in R?

后端 未结 1 430
伪装坚强ぢ
伪装坚强ぢ 2021-01-17 05:58

I have one input file which has one paragraph. I need to find the frequency of particular word in that paragraph.

cat file:

Text    Index
train is go         


        
1条回答
  •  时光说笑
    2021-01-17 06:13

    If you gave a little more info I could probably provide more info in return. Using qdap you could:

    library(qdap)
    
    dat <- readLines(n=5)
    train is good   1
    let the train come      5
    train is best   3
    i m great       3
    what is best    2
    
    dat <- do.call(rbind.data.frame, strsplit(dat, "   +"))
    
    colnames(dat) <- c("Text", "Index")
    
    termco(dat$Text, , " train ")
    
    ## > termco(dat$Text, , " train ")
    ##   all word.count     train
    ## 1 all         16 3(18.75%)
    

    You could probably do all the paragraphs at once with termco. For more on termco see this link.

    Alot of this depends on what's separating paragraphs, how you're reading it in, how things are indented etc.

    The poster found the following useful:

    length(gregexpr("the", "the dog ate the word the", fixed = TRUE)[[1]])

    0 讨论(0)
提交回复
热议问题