R:读取文本文件时出现问题 [英] R: Got problems in reading text file

查看:58
本文介绍了R:读取文本文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 R 中读取文本文件.代码曾经可以工作.但是当我想重新测试它时,它没有.

I want to read text file in R. The code used to work. But when I want to retest it, it didn't.

#There are several text files in file'Obama' and file 'Romney'
candidates<-c("Obama","Romney")
pathname<-"C:/txt"
s.dir<-sprintf("%s/%s",pathname,candidates)
article<-Corpus(DirSource(directory=s.dir,encoding="ANSI"))

它显示的错误是

Error in iconv(readLines(x, warn = FALSE), encoding, "UTF-8", "byte") : 
unsupported conversion from 'ANSI' to 'UTF-8' in codepage 936

此外,当我使用下面的代码尝试读取单个文本文件时:

Also, when I use the code below to try to read a single text file:

m<-"C:/txt/Romney/1.txt"
cc<-Corpus(DirSource(directory=m,encoding="ANSI"))

显示:

Error in DirSource(directory = m, encoding = "ANSI") : empty directory

文件路径存在,为什么会出现这个问题?

The file path exist, why I met this problem?

推荐答案

以下是您需要做的:

  1. 将文章<-Corpus(DirSource(directory=s.dir,encoding="ANSI")) 更改为以下内容:

文章 <- VCorpus(DirSource(directory = s.dir), readerControl = list(reader=readPlain))

article <- VCorpus(DirSource(directory = s.dir), readerControl = list(reader=readPlain))

  1. 在 cleanCorpus 函数中,将 corpus.tmp <- tm_map(corpus.tmp, tolower) 更改为以下内容:

corpus.tmp <- tm_map(corpus.tmp, content_transformer(tolower))

corpus.tmp <- tm_map(corpus.tmp, content_transformer(tolower))

注意content_transformer"函数的使用.

Pay attention to usage of "content_transformer" function.

完成上述操作后,您应该能够解决问题.

Once done with above, you should be able to fix the problem.

这篇关于R:读取文本文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆