使用R的累积字数

使用R的累积字数,r,rvest,R,Rvest,有没有办法获得文本的累积字数?我有一篇文章,我正试图分析,我想找到一个累积的总数的文字以及文字中某些单词的累积计数 目前我有3个独立的数据帧。第一个包含文本文档中的所有单词,一个“count”列(一直向下包含1)和一个“total”列(给出“count”列的累积和)。其他两个数据框完全相同,只是它们只包含我在文本中查找的特定单词的所有匹配项 我们的目标是绘制一个图,显示在整个文本中使用这两个特定单词的关系 感谢您的帮助。以下是我到目前为止的情况 URL <- 'http://shakesp

有没有办法获得文本的累积字数?我有一篇文章,我正试图分析,我想找到一个累积的总数的文字以及文字中某些单词的累积计数

目前我有3个独立的数据帧。第一个包含文本文档中的所有单词,一个“count”列(一直向下包含1)和一个“total”列(给出“count”列的累积和)。其他两个数据框完全相同,只是它们只包含我在文本中查找的特定单词的所有匹配项

我们的目标是绘制一个图,显示在整个文本中使用这两个特定单词的关系

感谢您的帮助。以下是我到目前为止的情况

URL <- 'http://shakespeare.mit.edu/romeo_juliet/full.html'
romeo <- htmlParse(URL)
txPath <- "//blockquote//a"
txValue <- xpathApply(romeo, txPath, xmlValue)
txValue <- strsplit(gsub('\\n','',txValue), split="  ")

words <- unlist(str_extract_all(txValue,'(\\w+)\'*(\\w+)'))
vWord <- tolower(words)
rCount <- unlist(str_extract_all(vWord,'(romeo)'))
lCount <- unlist(str_extract_all(vWord,'(love)'))

rDF <- as.data.frame(rCount) %>%
  mutate(count=1) %>%
  mutate(tot=cumsum(count))
lDF <- as.data.frame(lCount) %>%
  mutate(count=1) %>%
  mutate(tot=cumsum(count))
wordsDF <- as.data.frame(vWord) %>%
  mutate(count=1) %>%
  mutate(tot=cumsum(count))

URL如果您包含了数据和所需的输出,则会有所帮助。但根据我的理解,您是否可以使用“第一个”data.frame执行以下操作(通过
dplyr
):

我对您的“第一”数据帧的看法:

df <- data.frame(word = c("a", "b", "c", "a", "a", "c", "d", "b", "a"),
                 count = rep(1,9))

library(dplyr)
df %>% group_by(word) %>% mutate(cumsum= cumsum(count))
另外,因为我需要强迫自己学习
data.table
,这里有一个解决方案:

 library(data.table)
 setDT(df)[, cumsum:=cumsum(count), by=word]

如果你把你的数据和你想要的输出都包括进去,这会很有帮助。但根据我的理解,您是否可以使用“第一个”data.frame执行以下操作(通过
dplyr
):

我对您的“第一”数据帧的看法:

df <- data.frame(word = c("a", "b", "c", "a", "a", "c", "d", "b", "a"),
                 count = rep(1,9))

library(dplyr)
df %>% group_by(word) %>% mutate(cumsum= cumsum(count))
另外,因为我需要强迫自己学习
data.table
,这里有一个解决方案:

 library(data.table)
 setDT(df)[, cumsum:=cumsum(count), by=word]

这展示了如何使用
stringi
(比内置的string操作更快、更灵活)对语料库进行切片和切割,以及一种绘制所需比较的方法:

library(xml2)
library(rvest)
library(dplyr)
library(stringi)
library(ggplot2)

URL <- 'http://shakespeare.mit.edu/romeo_juliet/full.html'
wherefore <- read_html(URL)

txt <- stri_trim(html_text(html_nodes(wtxtherefore, "blockquote > a")))

corpus <- data_frame(word=stri_trans_tolower(unlist(stri_extract_all_words(txt))),
                     count=1)
corpus$word_number <- 1:nrow(corpus)

cumsum_corpus <- mutate(group_by(corpus, word), cumsum=cumsum(count))

gg <- ggplot(filter(cumsum_corpus, word %in% c("romeo", "juliet")),
             aes(x=word_number, y=cumsum))
gg <- gg + geom_line(aes(color=word), size=0.75)
gg <- gg + geom_point(aes(fill=word), shape=21, color="white", size=1.5)
gg <- gg + scale_x_continuous(limits=c(1, nrow(corpus)))
gg <- gg + theme_bw()
gg
库(xml2)
图书馆(rvest)
图书馆(dplyr)
图书馆(stringi)
图书馆(GG2)

URL这显示了如何使用
stringi
(比内置的字符串操作更快、更灵活)进行语料库切片和切分,以及绘制所需比较的一种方法:

library(xml2)
library(rvest)
library(dplyr)
library(stringi)
library(ggplot2)

URL <- 'http://shakespeare.mit.edu/romeo_juliet/full.html'
wherefore <- read_html(URL)

txt <- stri_trim(html_text(html_nodes(wtxtherefore, "blockquote > a")))

corpus <- data_frame(word=stri_trans_tolower(unlist(stri_extract_all_words(txt))),
                     count=1)
corpus$word_number <- 1:nrow(corpus)

cumsum_corpus <- mutate(group_by(corpus, word), cumsum=cumsum(count))

gg <- ggplot(filter(cumsum_corpus, word %in% c("romeo", "juliet")),
             aes(x=word_number, y=cumsum))
gg <- gg + geom_line(aes(color=word), size=0.75)
gg <- gg + geom_point(aes(fill=word), shape=21, color="white", size=1.5)
gg <- gg + scale_x_continuous(limits=c(1, nrow(corpus)))
gg <- gg + theme_bw()
gg
库(xml2)
图书馆(rvest)
图书馆(dplyr)
图书馆(stringi)
图书馆(GG2)

URL你能告诉我们你到目前为止尝试了什么吗?当然,我编辑了我的文章以包含我的代码。你能告诉我们你到目前为止尝试了什么吗?当然,我编辑了我的文章以包含我的代码。谢谢,我必须尝试一下。我还编辑了我的文章,加入了我的代码。谢谢,我必须尝试一下。我还编辑了我的文章,加入了我的代码。谢谢,这正是我在寻找的汉克斯,这正是我在寻找的