r - 如何从data.frame获取 "code for creating a variable"

标签 r function variables command 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

我现在正在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)
)

R中有这样的功能吗?

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

Shows the CREATE TABLE statement that creates the named table.



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

最佳答案

我认为您正在寻找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

关于r - 如何从data.frame获取 "code for creating a variable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19393865/

相关文章:

jquery - 阻止 jQuery 做它的事情?

c - 在 if 语句中声明变量 (ANSI C)

java - 从 DatePicker 更新 TextView

r - 如何更改点并向云图添加回归(使用 R)?

c++ - 重复 Rcpp NumericVector

r - 将矩阵拆分为 4 个子矩阵,它们的总和之间的差值最小

MySQL 错误 - "Not allowed to return a result set from a function"

vb.net - 对象变量或 With block 变量未设置错误 Visual Basic

java - 参数的变量作为参数

r - 如何使用 R 通过 FTP 将文件上传到服务器?