简体   繁体   English

如何将Txt文件转换为.Rdata格式

[英]How To Convert Txt file into .Rdata format

I am using my python script and Tweepy to collect tweets and extract only text portion. 我正在使用python脚本和Tweepy收集推文并仅提取文本部分。 I saved all the texts from these tweets in txt format. 我将这些推文中的所有文本保存为txt格式。

I am now trying to load this text in R for textual analysis using this module ( http://rpubs.com/gogamza/ko_text_mining ) However, this R module only accepts .Rdata files, and I somehow need to feed my tweet collection onto R. 我现在正尝试使用此模块( http://rpubs.com/gogamza/ko_text_mining )在R中加载此文本以进行文本分析。但是,此R模块仅接受.Rdata文件,因此我需要将我的tweet集合输入到R.

The R code says: R代码说:

library(twitteR)
# n <- 200
# 
# keyword <- '삼성전자'
# 
# keyword <- enc2utf8(keyword)
# 
# rdmTweets <- searchTwitter(keyword, n)

load(url("http://dl.dropbox.com/u/8686172/twitter.RData"))

nDocs <- length(rdmTweets)

So, inside the load command, I'd like to add my own text file there. 因此,在load命令中,我想在其中添加自己的文本文件。
Can someone offer me a tip? 有人可以给我小费吗? Thanks a lot.. I'd appreciate it so much. 非常感谢..非常感谢。

You can load the data in your txt file in R and save it as an RData file: 您可以将数据加载到R中的txt文件中,并将其另存为RData文件:

sapply(list.files("/folderWithFiles", pattern="*.txt", full.names = TRUE),
       function(x) { myDataInVector = scan(x, what=character()); save(myDataInVector, file=gsub("\\.txt","\\.RData",x))  } )

The reading in part scan(x, what=character()) assumes you have your data in all in the text file as a space delimited vector. 部分scan(x, what=character())的读数假定您在文本文件中以空格分隔的向量形式存储了所有数据。 You can use read.table or change the arguments of the scan command if it is in a different format. 您可以使用read.table或更改scan命令的参数(如果其格式不同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM