在R中将RDA写入CSV [英] Writing an RDA to CSV in R

查看:2288
本文介绍了在R中将RDA写入CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个脚本来加载一个RDA的文件名,并写一个等效的文件作为CSV。

I'm trying to write a script to load an RDA by filename and write an equivalent file out as CSV.

几乎有加载和写入)但是输出CSV包含load()返回的字符串向量,而不是load ...引用的数据框。

It's almost there (with the loading and writing) however the output CSV contains the vector of strings returned by load(), rather than the data frame referenced by load...

$ cat convert.r
#!/usr/bin/env Rscript

argv <- commandArgs(TRUE)
inFile <- toString(argv[1])
print(inFile)

outFile <- gsub(".rda$", ".csv", inFile)
print(outFile)

inData <- load(inFile)
write.csv(inData, file=outFile)

这是命令+输出...

This is the command + output...

/convert.r data.rda
[1] "data.rda"
[1] "data.csv"
[1] "table.data"

所以你可以看到它从参数中提取输入文件名,它创建正确的输出文件名,但inData是一个称为table.data的全局对象的引用。当write.csv运行时,它只包含:

So you can see its picking up the input filename from the arguments, it's creating the right output filename, but the inData is a reference to the global object called table.data. When write.csv runs, it just contains this:

$ cat data.csv
"","x"
"1","table.data"

从rda文件启动数据帧?我知道,如果RDA包含多个框架 - 也许它应该循环并执行file-frame.csv?有一个当前的风险。

How do I make write.csv pick up the data-frame from the rda file? I understand there is a current risk if the RDA contains more than one frame - maybe it should loop over them and do file-frame.csv?!

推荐答案

显然,解决问题的最好方法是公开提出问题。

Clearly the best way to solve a problem yourself is to publically ask a question.

答案是: get

inData <- load(inFile)
write.csv(get(inData), file=outFile)

(答案在 http://stackoverflow.com/a/6316461/224707 greg-snow

这篇关于在R中将RDA写入CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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