全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
24044 5
2015-05-09
想做个层次聚类,转置后设置行名时提示Error in `rownames<-`(`*tmp*`, value = c(271L, 247L, 342L, 1158L, 1338L,  :
  length of 'dimnames' [1] not equal to array extent
plot的时候又提示
Error in graphics:::plotHclust(n1, merge, height, order(x$order), hang,  :
  谱系图输入不对

以下是代码和数据,望各位大神帮忙看看,在下新手,多谢多谢!
body1.txt
大小:(4.15 MB)

 马上下载


> csv <- read.table("body1.txt",header=T)
> mystopwords<- unlist (read.table("StopWords.txt",stringsAsFactors=F))
> library(tm)
> #移除数字
> removeNumbers = function(x) { ret = gsub("[0-90123456789]","",x) }
> #中文分词,也可以考虑使用rmmseg4j、rsmartcn
> wordsegment<- function(x) {
+     library(Rwordseg)
+     segmentCN(x)
+ }
> #去除停止词,效果比较差,可以进一步完善
> removeStopWords = function(x,words) {
+     ret = character(0)
+     index <- 1
+     it_max <- length(x)
+     while (index <= it_max) {
+         if (length(words[words==x[index]]) <1) ret <- c(ret,x[index])
+         index <- index +1
+     }
+     ret
+ }
> Sys.setenv(JAVA_HOME='C:/Program Files/Java/jdk1.6.0_43/jre')
> sample.words <- lapply(csv, removeNumbers)
> sample.words <- lapply(sample.words, wordsegment)
> #先处理中文分词,再处理stopwords,防止全局替换丢失信息
> sample.words <- lapply(sample.words, removeStopWords, mystopwords)
> #构建语料库
> corpus = Corpus(VectorSource(sample.words))
>
> sample.dtm <- DocumentTermMatrix(corpus, control = list(wordLengths = c(2, Inf)))
> sample_matrix =as.matrix(sample.dtm)
> rownames(sample_matrix)<- csv$newsid
Error in `rownames<-`(`*tmp*`, value = c(271L, 247L, 342L, 1158L, 1338L,  :
  length of 'dimnames' [1] not equal to array extent
>
> dtm2 = removeSparseTerms(sample.dtm, sparse=0.99)
> d <- dist(dtm2, method = "euclidean")
> fit <- hclust(d, method="ward.D")
> plot(fit)
Error in graphics:::plotHclust(n1, merge, height, order(x$order), hang,  :
  谱系图输入不对


还想问问大家,由于我的数据量比较大,做层次聚类的谱系图会太密集,看不清,有什么更好的文本聚类方法吗?我后续还想进行如用cutree提取出每条新闻属于哪一类的操作,不知道有没有别的方法或函数能做到的,多谢大家了!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2015-5-9 14:43:13
自己顶,快来帮忙啊
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-5-9 20:07:03
矩阵行数跟newsid长度不一样
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-5-9 21:26:33
katymeala 发表于 2015-5-9 20:07
矩阵行数跟newsid长度不一样
这是什么情况啊,是前边的步骤影响数据了吗?我这个是照着网上的一个例子写的,他的数据格式都跟我的很像,我就按照自己的数据改了改,可是就是不行.........
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-3-7 17:20:09
nrow(sample_matrix)
rownames(sample_matrix) <- csv$type[1:95]

我是这样干的
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2022-9-5 16:37:34
请问您第二个问题是怎末解决的呢
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群