如何从data.frame获取"创建变量的代码"

R_U*_*ser 6 variables command r function creation

我有一个data.frame(例如使用数据文件读取read.delim),其中包含以下数据:

'data.frame':   10 obs. of  3 variables:
 $ x : int  1 2 3 4 5 6 7 8 9 10
 $ y1: num  3 5 1 8 2 4 5 0 8 2
 $ y2: num  1 8 2 1 4 5 3 0 8 2
Run Code Online (Sandbox Code Playgroud)

我现在正在寻找R中的一个函数,它接受变量(data)并打印出一个可能的命令来创建一个变量data.在这种情况下,它应该打印出来:

data <- data.frame(
  x=c(1:10)
  , y1=c(3,5,1,8,2,4,5,0,8,2)
  , y2=c(1,8,2,1,4,5,3,0,8,2)
)
Run Code Online (Sandbox Code Playgroud)

在R中是否有这样的功能

在MySQL中存在例如命令SHOW CREATE TABLE tbl_name,其中

显示创建命名表的CREATE TABLE语句.

我正在为R中的变量寻找类似的东西

mri*_*rip 12

我想你正在寻找dput:

> dput(data)
structure(list(x = 1:10, y1 = c(3, 5, 1, 8, 2, 4, 5, 0, 8, 2), 
    y2 = c(1, 8, 2, 1, 4, 5, 3, 0, 8, 2)), .Names = c("x", "y1", 
"y2"), row.names = c(NA, -10L), class = "data.frame")


> data0<-structure(list(x = 1:10, y1 = c(3, 5, 1, 8, 2, 4, 5, 0, 8, 2), 
+     y2 = c(1, 8, 2, 1, 4, 5, 3, 0, 8, 2)), .Names = c("x", "y1", 
+ "y2"), row.names = c(NA, -10L), class = "data.frame")
> identical(data,data0)
[1] TRUE
Run Code Online (Sandbox Code Playgroud)


Ric*_*rta 5

dput是您可能正在寻找的基本功能.
我使用了一个包装函数,reproduce我在"如何制作可重现的示例"问题上分享了这个函数.

# install.packages("devtools")
library(devtools)
source_url("https://raw.github.com/rsaporta/pubR/gitbranch/reproduce.R")

reproduce(myData, whole=TRUE)
Run Code Online (Sandbox Code Playgroud)

这使:

 data <- structure(list(x = 1:10, y1 = c(3, 5, 1, 8, 2, 4, 5, 0, 8, 2),y2 = c(1, 8, 2, 1, 4, 5, 3, 0, 8, 2)), .Names = c("x", "y1","y2"), row.names = c(NA, -10L), class = "data.frame") 
Run Code Online (Sandbox Code Playgroud)

您还可以输出选择的行,列等或它们的百分比.

有关该功能的进一步说明,请查看r faq问题