如何将R对象的定义导出为纯文本,以便其他人可以重新创建它? [英] How to export the definition of an R object to plain text so that others can recreate it?

查看:178
本文介绍了如何将R对象的定义导出为纯文本,以便其他人可以重新创建它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你在R中有这个数据,你想在stackoverflow上发一个问题。对于其他人来说,最好的帮助你,如果他们可以拥有你的对象(数据框,向量等)的副本,那将是很好的。



在一个名为site.data的数据框中

 > site.data 
网站年度峰值
1 ALBEN 5 101529.6
2 ALBEN 10 117483.4
3 ALBEN 20 132960.9
8 ALDER 5 6561.3
9 ALDER 10 7897.1
10 ALDER 20 9208.1
15 AMERI 5 43656.5
16 AMERI 10 51475.3
17 AMERI 20 58854.4

如何打包,以便用户可以根据您的需要重新创建数据?



如果没有人下载文本文件并将其导入,您可以这样做。



(注意:这些数据是从REvolutions博客的一个例子中分析的)

解决方案

dput命令写入ASCII表示。如果不是一个文件名,你会把它写入控制台。

 > dput(site.data,)
structure(list(site = structure(c(1L,1L,1L,2L,2L,2L,3L,
3L,3L),.Label = c (ALBEN,ALDER,AMERI),class =factor),
year = c(5L,10L,20L,5L,10L,20L,5L,10L,20L) c(101529.6,
117483.4,132960.9,6561.3,7897.1,9208.1,4366.5,51475.3,
58854.4)),.Names = c(site,year,peak),行。 name = c(1L,
2L,3L,8L,9L,10L,15L,16L,17L),class =data.frame)

只需复制结构并将其放在示例代码中的site.data =之后,人们将能够完全按照您的要求重新创建数据框。 p>

Let's say you have this data in R, and you want to post a question on stackoverflow. For others to best help you, it would be nice if they could have a copy of your object (dataframe, vector, etc) to work with.

Let's say your data is in a data frame called site.data

> site.data
    site year     peak
1  ALBEN    5 101529.6
2  ALBEN   10 117483.4
3  ALBEN   20 132960.9
8  ALDER    5   6561.3
9  ALDER   10   7897.1
10 ALDER   20   9208.1
15 AMERI    5  43656.5
16 AMERI   10  51475.3
17 AMERI   20  58854.4

How do you package it up so that the users can recreate the data exactly as you have it?

You want to do this without having people download a text file and import it.

(Note: These data subsetted from an example of the REvolutions blog)

解决方案

The dput command writes an ASCII representation. If instead of a filename you put "" it will write it to the console

> dput(site.data,"")
structure(list(site = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L), .Label = c("ALBEN", "ALDER", "AMERI"), class = "factor"), 
    year = c(5L, 10L, 20L, 5L, 10L, 20L, 5L, 10L, 20L), peak = c(101529.6, 
    117483.4, 132960.9, 6561.3, 7897.1, 9208.1, 43656.5, 51475.3, 
    58854.4)), .Names = c("site", "year", "peak"), row.names = c(1L, 
2L, 3L, 8L, 9L, 10L, 15L, 16L, 17L), class = "data.frame")

Just copy the structure and put it after "site.data=" in your example code and people will be able to recreate the data frame exactly as you have it.

这篇关于如何将R对象的定义导出为纯文本,以便其他人可以重新创建它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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