Aggregate.data.frame(as.data.frame(x),...)中的错误:参数必须具有相同的长度

时间:2019-02-27 17:12:23

标签: r text-mining topic-modeling

您好,我正在处理本教程中的最后一个示例:主题随时间变化的比例。 https://tm4ss.github.io/docs/Tutorial_6_Topic_Models.html

我使用此代码为数据运行它

library(readxl)
library(tm)
# Import text data

tweets <- read_xlsx("C:/R/data.xlsx")

textdata <- tweets$text

#Load in the library 'stringr' so we can use the str_replace_all function. 
library('stringr')

#Remove URL's 
textdata <- str_replace_all(textdata, "https://t.co/[a-z,A-Z,0-9]*","")


textdata <- gsub("@\\w+", " ", textdata)  # Remove user names (all proper names if you're wise!)

textdata <- iconv(textdata, to = "ASCII", sub = " ")  # Convert to basic ASCII text to avoid silly characters
textdata <- gsub("#\\w+", " ", textdata)

textdata <- gsub("http.+ |http.+$", " ", textdata)  # Remove links

textdata <- gsub("[[:punct:]]", " ", textdata)  # Remove punctuation


#Change all the text to lower case
textdata <- tolower(textdata)



#Remove Stopwords. "SMART" is in reference to english stopwords from the SMART information retrieval system and stopwords from other European Languages.
textdata <- tm::removeWords(x = textdata, c(stopwords(kind = "SMART")))


textdata <- gsub(" +", " ", textdata) # General spaces (should just do all whitespaces no?)

# Convert to tm corpus and use its API for some additional fun
corpus <- Corpus(VectorSource(textdata))  # Create corpus object


#Make a Document Term Matrix
dtm <- DocumentTermMatrix(corpus)

ui = unique(dtm$i)
dtm.new = dtm[ui,]

#Fixes this error: "Each row of the input matrix needs to contain at least one non-zero entry" See: https://stackoverflow.com/questions/13944252/remove-empty-documents-from-documenttermmatrix-in-r-topicmodels
#rowTotals <- apply(datatm , 1, sum) #Find the sum of words in each Document
#dtm.new   <- datatm[rowTotals> 0, ]

library("ldatuning")
library("topicmodels")

k <- 7

ldaTopics <- LDA(dtm.new, method = "Gibbs", control=list(alpha = 0.1, seed = 77), k = k)


#####################################################
#topics by year

tmResult <- posterior(ldaTopics)
tmResult
theta <- tmResult$topics
dim(theta)
library(ggplot2)
terms(ldaTopics, 7)

tweets$decade <- paste0(substr(tweets$date2, 0, 3), "0")

topic_proportion_per_decade <- aggregate(theta, by = list(decade = tweets$decade), mean)


top5termsPerTopic <- terms(topicModel, 7)
topicNames <- apply(top5termsPerTopic, 2, paste, collapse=" ")

# set topic names to aggregated columns
colnames(topic_proportion_per_decade)[2:(K+1)] <- topicNames


# reshape data frame
vizDataFrame <- melt(topic_proportion_per_decade, id.vars = "decade")

# plot topic proportions per deacde as bar plot
require(pals)
ggplot(vizDataFrame, aes(x=decade, y=value, fill=variable)) + 
  geom_bar(stat = "identity") + ylab("proportion") + 
  scale_fill_manual(values = paste0(alphabet(20), "FF"), name = "decade") + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

这是输入数据的excel文件 https://www.mediafire.com/file/4w2hkgzzzaaax88/data.xlsx/file

使用聚合函数运行该行时出现错误,我无法确定聚合发生了什么,我创建了与tutoria相同的“ decade”变量,将其显示并看上去好的,theta变量也可以..我根据这篇文章修改了几次聚合函数  Error in aggregate.data.frame : arguments must have same length

但是仍然有相同的错误..请帮助

1 个答案:

答案 0 :(得分:2)

我不确定您要使用该命令实现什么

topic_proportion_per_decade <- aggregate(theta, by = list(decade = tweets$decade), mean)

据我所知,您只能用十年来产生

tweets$decade <- paste0(substr(tweets$date2, 0, 3), "0")
table(tweets$decade)

2010 
3481 

tweetstextdata的所有预处理过程中,都会产生一些空行。这是您的问题开始的地方。 带有新的空行的文本数据是您的corpusdtm的基础。您可以通过以下行摆脱它们:

ui = unique(dtm$i)
dtm.new = dtm[ui,]

与此同时,您基本上要删除dtm中的空列,从而更改对象的长度。没有空单元格的新dtm是 然后是主题模型的新基础。当您尝试对两个不同长度的对象使用aggregate()时,这又困扰了您:tweets$decade,仍然是theta的3418的旧长度,它是由主题模型,该模型又基于dtm.new -请记住,行数较少的模型。

我建议首先在tweets中获得一个ID列。以后,您可以使用ID来查找以后会被预处理删除的文本,并匹配tweet$decadetheta的长度。

我重写了您的代码-试试看:

library(readxl)
library(tm)
# Import text data

tweets <- read_xlsx("data.xlsx")

## Include ID for later
tweets$ID <- 1:nrow(tweets)

textdata <- tweets$text

#Load in the library 'stringr' so we can use the str_replace_all function. 
library('stringr')

#Remove URL's 
textdata <- str_replace_all(textdata, "https://t.co/[a-z,A-Z,0-9]*","")


textdata <- gsub("@\\w+", " ", textdata)  # Remove user names (all proper names if you're wise!)

textdata <- iconv(textdata, to = "ASCII", sub = " ")  # Convert to basic ASCII text to avoid silly characters
textdata <- gsub("#\\w+", " ", textdata)

textdata <- gsub("http.+ |http.+$", " ", textdata)  # Remove links

textdata <- gsub("[[:punct:]]", " ", textdata)  # Remove punctuation

#Change all the text to lower case
textdata <- tolower(textdata)

#Remove Stopwords. "SMART" is in reference to english stopwords from the SMART information retrieval system and stopwords from other European Languages.
textdata <- tm::removeWords(x = textdata, c(stopwords(kind = "SMART")))

textdata <- gsub(" +", " ", textdata) # General spaces (should just do all whitespaces no?)

# Convert to tm corpus and use its API for some additional fun
corpus <- Corpus(VectorSource(textdata))  # Create corpus object

#Make a Document Term Matrix
dtm <- DocumentTermMatrix(corpus)
ui = unique(dtm$i)
dtm.new = dtm[ui,]

#Fixes this error: "Each row of the input matrix needs to contain at least one non-zero entry" See: https://stackoverflow.com/questions/13944252/remove-empty-documents-from-documenttermmatrix-in-r-topicmodels
#rowTotals <- apply(datatm , 1, sum) #Find the sum of words in each Document
#dtm.new   <- datatm[rowTotals> 0, ]

library("ldatuning")
library("topicmodels")

k <- 7

ldaTopics <- LDA(dtm.new, method = "Gibbs", control=list(alpha = 0.1, seed = 77), k = k)

#####################################################
#topics by year

tmResult <- posterior(ldaTopics)
tmResult
theta <- tmResult$topics
dim(theta)
library(ggplot2)
terms(ldaTopics, 7)

id <- data.frame(ID = dtm.new$dimnames$Docs)
colnames(id) <- "ID"
tweets$decade <- paste0(substr(tweets$date2, 0, 3), "0")

tweets_new <- merge(id, tweets, by.x="ID", by.y = "ID", all.x = T)

topic_proportion_per_decade <- aggregate(theta, by = list(decade = tweets_new$decade), mean)