我对此很困惑,我确信这很简单,希望有人能指出我的正确方向。为什么我的R函数一步一步地工作,但没有作为执行时运行
我正在使用TM包进行文本挖掘项目,当我在控制台中运行代码时,它完美地工作,但是当我调用函数本身时,最终输出为空。
下面是一些示例代码:
func <- function(filename, count=100, full=FALSE){
packages <- c("ggplot2", "tm")
if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
install.packages(setdiff(packages, rownames(installed.packages())))
}
library(tm)
library(ggplot2)
## get data
data <- read.csv(filename)
##Create corpus and remove formatting from text
Tickets <- Corpus(DataframeSource(data))
Tickets = tm_map(Tickets, removePunctuation)
Tickets = tm_map(Tickets, tolower)
##Create stopwords vector to remove complete list from data
stopwords <- read.csv("stopwords.csv", header=FALSE)
stopwords <- as.character(stopwords[,1])
stopwords <- c(stopwords("english"), stopwords)
## create full analasis of whole data, if selected by user
if(full==TRUE){
Tickets = tm_map(Tickets, PlainTextDocument) ##convert back to a text document we can analyse
Tickets.TDM <- TermDocumentMatrix(Tickets) ## create matrix for analysis
TDM.frame <- data.frame(as.matrix(Tickets.TDM))
write.csv(TDM.frame, "Full_queue_analysis.csv")
}
## Remove Stopwords and irrelevant data then convert to TDM for analysis
Tickets = tm_map(Tickets, removeWords, stopwords)
Tickets = tm_map(Tickets, removeNumbers)
Tickets = tm_map(Tickets, stripWhitespace)
Tickets = tm_map(Tickets, PlainTextDocument)
Tickets.TDM <- TermDocumentMatrix(Tickets)
## matrix to frame for additional calculations
TDM.frame <- data.frame(as.matrix(Tickets.TDM))
##count each word once word per entry and only display those which count more than user specified amount
Counts.df <- data.frame(rowSums(TDM.frame > 0))
colnames(Counts.df) <- "count"
Counts.df <- subset(Counts.df, count > count)
## create csv file for final counts
write.csv(Counts.df, "Queue_analysis.csv")
##Print basic analysis based on user option
cat("Terms which appear more than",count,"times:")
findFreqTerms(Tickets.TDM, count)
事情似乎出问题的初始化向量Counts.df的时候,我完全可以通过控制台执行这一点,它用正确的数据填充然而,当在运行功能可按它是完全空的,尽管它确实存在。
没有错误,函数按预期结束,但打开csv文件时,只有“count”标题为空。
感谢您的任何建议!
编辑 - 添加功能本身,对不起!
您需要显示实际功能以及如何调用它。 – joran 2015-03-30 19:39:21
愚蠢的我,你是完全正确的:D我已经完成了它,谢谢! – MissMoo 2015-03-30 19:43:04