楼主: 我就是只猫
24232 5

[问答] R语言 错误“dimnames”[ 1 ]必需与陈列范围相等及谱系图输入不对 [推广有奖]

  • 0关注
  • 1粉丝

已卖:3份资源

大专生

43%

还不是VIP/贵宾

-

威望
0
论坛币
110 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
326 点
帖子
57
精华
0
在线时间
39 小时
注册时间
2014-7-16
最后登录
2015-5-23

楼主
我就是只猫 发表于 2015-5-9 11:18:05 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
想做个层次聚类,转置后设置行名时提示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提取出每条新闻属于哪一类的操作,不知道有没有别的方法或函数能做到的,多谢大家了!
二维码

扫码加我 拉你入群

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

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

关键词:Names name R语言 mes DIM Error 中考

沙发
我就是只猫 发表于 2015-5-9 14:43:13
自己顶,快来帮忙啊

藤椅
katymeala 发表于 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]

我是这样干的

地板
LiuLiuYX123 发表于 2022-9-5 16:37:34
请问您第二个问题是怎末解决的呢

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-1 22:01