报告语料库文档中的平均字符数
Report the mean number of characters in Corpus document
所以我有一个语料库设置读取一堆包含段落的文本文件。
library('tm')
my.text.location <- "C:/Users//.../*/"
apapers <- VCorpus(DirSource(my.text.location))
现在我需要找出每个文本中字符的平均值。 运行一个
mean(nchar(apapers), na.rm =T)
导致非常奇怪的输出,超过了字符数。
还有其他获取平均值的方法吗?
您没有提供可重现的示例,但 rowMeans(sapply(apapers, nchar))
将 return 所有文档的平均字符数。 "Content" 是您需要的栏目。
更长的版本是运行对语料库的应用,计算每个文档的数量。转置此数据并将其变成 data.frame。 data.frame 将包含两列,内容和元。内容是您需要的。取内容列的平均值将为您提供文档中的平均字符数。这样做的好处是您可以使用 table 以备不时之需。
# your code
my_count <- data.frame(t(sapply(apapers, nchar)))
mean(my_count$content)
使用原始数据集的可重现示例:
library(tm)
data("crude")
crude <- as.VCorpus(crude)
# in one statement
rowMeans(sapply(crude, nchar))
content meta
1220.30 453.15
# longer version keeping intermediate results.
my_count <- data.frame(t(sapply(crude, nchar)))
mean(my_count$content)
[1] 1220.3
my_count
content meta
127 527 440
144 2634 458
191 330 444
194 394 441
211 552 441
236 2774 455
237 2747 477
242 930 453
246 2115 440
248 2066 466
273 2241 458
349 593 492
352 621 468
353 591 445
368 629 440
489 876 445
502 1166 446
543 463 447
704 1797 456
708 360 451
所以我有一个语料库设置读取一堆包含段落的文本文件。
library('tm')
my.text.location <- "C:/Users//.../*/"
apapers <- VCorpus(DirSource(my.text.location))
现在我需要找出每个文本中字符的平均值。 运行一个
mean(nchar(apapers), na.rm =T)
导致非常奇怪的输出,超过了字符数。
还有其他获取平均值的方法吗?
您没有提供可重现的示例,但 rowMeans(sapply(apapers, nchar))
将 return 所有文档的平均字符数。 "Content" 是您需要的栏目。
更长的版本是运行对语料库的应用,计算每个文档的数量。转置此数据并将其变成 data.frame。 data.frame 将包含两列,内容和元。内容是您需要的。取内容列的平均值将为您提供文档中的平均字符数。这样做的好处是您可以使用 table 以备不时之需。
# your code
my_count <- data.frame(t(sapply(apapers, nchar)))
mean(my_count$content)
使用原始数据集的可重现示例:
library(tm)
data("crude")
crude <- as.VCorpus(crude)
# in one statement
rowMeans(sapply(crude, nchar))
content meta
1220.30 453.15
# longer version keeping intermediate results.
my_count <- data.frame(t(sapply(crude, nchar)))
mean(my_count$content)
[1] 1220.3
my_count
content meta
127 527 440
144 2634 458
191 330 444
194 394 441
211 552 441
236 2774 455
237 2747 477
242 930 453
246 2115 440
248 2066 466
273 2241 458
349 593 492
352 621 468
353 591 445
368 629 440
489 876 445
502 1166 446
543 463 447
704 1797 456
708 360 451