我有一个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/