Error in nchar(Terms(x), type = “chars”) : invalid multibyte string, element 204, when inspecting document term matrix

安稳与你 提交于 2021-02-11 13:45:16

问题


Here is the source code that I have used:

 MyData <- Corpus(DirSource("F:/Data/CSV/Data"),readerControl = list(reader=readPlain,language="cn"))
    SegmentedData <- lapply(MyData, function(x) unlist(segmentCN(x)))
    temp <- Corpus(DataframeSource(SegmentedData), readerControl = list(reader=readPlain, language="cn"))

Preprocessing Data

temp <- tm_map(temp, removePunctuation)
temp <- tm_map(temp,removeNumbers)
removeURL <- function(x)gsub("http[[:alnum:]]*"," ",x)
temp <- tm_map(temp, removeURL)
temp <- tm_map(temp,stripWhitespace)
dtmxi <- DocumentTermMatrix(temp)
dtmxi <- removeSparseTerms(dtmxi,0.83)

**inspect(t(dtmxi))** ---This is where I get the error

回答1:


I believe there are some Chinese characters in your file. To overcome this issue, use this line of code to read them as well:

Sys.setlocale('LC_ALL','C')



回答2:


My RStudio restarts the session after to set Sys.setlocale( 'LC_ALL','C' ) and run the TermDocumentMatrix( mycorpus ) function.



来源:https://stackoverflow.com/questions/51269083/error-in-nchartermsx-type-chars-invalid-multibyte-string-element-204

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!