as.data.frame.default(dtm) 中的错误:无法将类 "c("DocumentTermMatrix", "simple_triplet_ma
Posted
技术标签:
【中文标题】as.data.frame.default(dtm) 中的错误:无法将类 "c("DocumentTermMatrix", "simple_triplet_matrix")" 强制转换为 data.frame【英文标题】:Error in as.data.frame.default(dtm) : cannot coerce class "c("DocumentTermMatrix", "simple_triplet_matrix")" to a data.frame 【发布时间】:2014-11-30 12:05:00 【问题描述】:我有类似的问题,我从网上下载了一个大的推文文件 将其保存为 data.txt 并使用 rstudio(导入数据集)加载到 R 中。 但有错误,无法继续。
This is step by step on what i did and the errors i had.
# required packages
library(twitteR)
library(plyr)
library(ggplot2)
library(wordcloud)
library(RColorBrewer)
library(tm)
library(XML)
library(SnowballC)
data<- read.csv("~/data/datasStream.txt", header=FALSE , sep = "," )
我有 3425 个观察值和 97 个变量
## i load it to corpus
corpus = Corpus(VectorSource(data)) ## 97 elements 17.4 MB
## i cleaned the data using
corpus = tm_map (corpus, tolower)
corpus = tm_map (corpus, stripWhitespace)
corpus = tm_map (corpus, stemDocument)
corpus = tm_map (corpus, PlainTextDocument)
# remove unnecessary spaces
corpus = gsub("[ \t]2,", "", corpus)
corpus = gsub("^\\s+|\\s+$", "", corpus)
# remove NAs in corpus
corpus = corpus[!is.na(corpus)]
dtm = DocumentTermMatrix(corpus)
dtm
<<DocumentTermMatrix (documents: 97, terms: 151132)>>
Non-/sparse entries: 201231/14458573
Sparsity : 99%
Maximal term length: 1775
Weighting : term frequency (tf)
adtm <- removeSparseTerms(dtm, 0.75)
adtm
<<DocumentTermMatrix (documents: 97, terms: 270)>>
Non-/sparse entries: 11962/14228
Sparsity : 54%
Maximal term length: 33
Weighting : term frequency (tf)
df1 = as.data.frame (m=as.matrix (adtm))
Error in as.data.frame.default(dtm) : cannot coerce class "c("DocumentTermMatrix", "simple_triplet_matrix")" to a data.frame
我该如何解决这个问题?我想对数据执行 k-means 聚类和词云。
这是一个示例数据:
"created_at":"2013 年 2 月 27 日星期三 14:24:12 +0000","id":306771719996186625,"id_str":"306771719996186625","text":"@Joeypearce 我们又来了一个铃铛看看我要帮我打扫的车 :-/ 下班后再见!X","source":"\u003ca href=\"http://twitter.com/download/iphone\" rel = \“nofollow \ \ \ u003e”,“截断”:false,“in_reply_to_status_id”:“in_reply_to_status_id_str”,“in_reply_to_user_id”:127665137,“in_reply_to_user_id_str”:“ 127665137","in_reply_to_screen_name":"Joeypearce","user":"id":274997668,"id_str":"274997668","name":"Ell Beaton \u00a9","screen_name":"Ell_Beaton"," location":"","url":null,"description":"去格伦,或者回家。","protected":false,"followers_count":147,"friends_count":85,"listed_count":0, "created_at":"Thu Mar 31 12:44:39 +0000 2011","favourites_count":132,"utc_offset":0,"time_zone":"London","geo_enabled":true,"verified":false, "statuses_count":1087,"lang":"en","contributors_ena流血":false,"is_translator":false,"profile_background_color":"1A1B1F","profile_background_image_url":"http://a0.twimg.com/profile_background_images/768018009/7a0b3fe303f234e8d6a5429bb9ede9a9.jpeg","profile_background_image_url_https":"https: //si0.twimg.com/profile_background_images/768018009/7a0b3fe303f234e8d6a5429bb9ede9a9.jpeg","profile_background_tile":true,"profile_image_url":"http://a0.twimg.com/profile_images/3304123896/606a7413bce208a1a38b1eb41fd017c9_normal.jpeg","profile_image_url_https” :"https://si0.twimg.com/profile_images/3304123896/606a7413bce208a1a38b1eb41fd017c9_normal.jpeg","profile_banner_url":"https://si0.twimg.com/profile_banners/274997668/1361751912","0"profile_link_color":"E5 ,"profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"252429","profile_text_color":"666666","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null," follow_request_sent":null,"notifications":null,"geo":"type":"Point","coor dinates":[52.43718380,-2.14324244],"coordinates":"type":"Point","coordinates":[-2.14324244,52.43718380],"place":"id":"ddeec3dc241e5b6a"," url":"http://api.twitter.com/1/geo/id/ddeec3dc241e5b6a.json","place_type":"city","name":"Dudley","full_name":"Dudley, Dudley" ,"country_code":"GB","country":"United Kingdom","bounding_box":"type":"Polygon","coordinates":[[[-2.191947,52.426012],[-2.191947,52.558221] ,[-2.011849,52.558221],[-2.011849,52.426012]]],"attributes":,"contributors":null,"retweet_count":0,"entities":"hashtags":[], "urls":[],"user_mentions":["screen_name":"Joeypearce","name":"Joey Pearce","id":127665137,"id_str":"127665137","indices":[0 ,11]],"favorited":false,"retweeted":false,"filter_level":"medium"
【问题讨论】:
我无法复制这个。如果没有可重复的示例(包括数据),充其量是很难提供帮助的。 我已经包含了一个示例数据。 你能把dput(adtm)
的结果贴出来吗?
【参考方案1】:
这是 R 文本挖掘的痛点之一。 dtm
和后续的 adtm
都有两种类类型。
class(dtm)
[1] "DocumentTermMatrix" "simple_triplet_matrix"
对于转置,术语文档矩阵也是如此。您可以通过首先将 dtm
,adtm
(或者如果您制作了 tdm
)转换为矩阵来解决此问题。我在 1000 条推文上对此进行了测试,并且能够进行强制转换。
adtm.m<-as.matrix(adtm)
adtm.df<-as.data.frame(adtm.m)
或者你可以嵌套函数:
adtm.df<-as.data.frame(as.matrix(adtm))
这有点笨拙,但要完成工作,您可以在此处查看新课程。
class(adtm.df)
[1] "data.frame"
【讨论】:
【参考方案2】:这是因为R
的强制代码,恕我直言,拒绝尝试将任意类转换为数据帧。有两个原因。通常的一个是有问题的类可能是“衣衫褴褛的”,即任何试图变成data.frame
的尝试都会产生不等长度的行或列。第二个原因是可能根本没有为对象定义强制方法有问题,这将是编写有问题的包的人的错。据我所知,这种情况要少得多。
您可能必须手动(例如通过循环或其他构造)提取对象内的记录并弄清楚如何构建类似矩阵的对象。
【讨论】:
感谢您的回复,数据是非结构化的,即我下载了一些推文文件。从上面的代码来看,在我想做数据框之前似乎没问题。有没有其他选择? 它可能没有结构,但肯定有class
属性。看起来您可能需要阅读 R
的基础知识。目前,请输入dput( adtm)
并将部分结果复制到您的问题中;也是str(adtm)
的结果。这将使我们能够看到发生了什么。
嗨 Carl,str(adtm) 6 个列表 $ i : int [1:12616] 1 1 1 1 1 1 1 1 1 1 ... $ j : int [1:12616] 1 2 3 5 6 8 9 10 11 12 ... $ v : num [1:12616] 3171 1 21 11 1 ... $ nrow : int 97 $ ncol : int 287 $ dimnames:List of 2 ..$ Docs : chr [1:97] "character(0)" "character(0)" "character(0)" "character(0)" ... ..$ 术语:chr [1:287] "0000" "012118" "012119" "012120" ... - attr(, "class")= chr [1:2] "DocumentTermMatrix" "simple_triplet_matrix" - attr(, "weighting")= chr [1: 2]“词频”“tf”>无法弄清楚 dput(adtm) 中的内容,因为它是 # 和单词的混合体。交易
粗略地说,dput()
将对象(如您的adtm
)转换为代码,任何人都可以使用该代码来读取此代码以生成相同的对象(有关详细信息,请参阅?dput
)。以上是关于as.data.frame.default(dtm) 中的错误:无法将类 "c("DocumentTermMatrix", "simple_triplet_ma的主要内容,如果未能解决你的问题,请参考以下文章