as.data.frame(x)和data.frame(x)之间的区别 [英] Difference between as.data.frame(x) and data.frame(x)
本文介绍了as.data.frame(x)和data.frame(x)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
as.data.frame(x)和data.frame(x)有什么区别
What is the difference between as.data.frame(x) and data.frame(x)
在以下示例中,结果与
x <- matrix(data=rep(1,9),nrow=3,ncol=3)
> x
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 1
[3,] 1 1 1
> data.frame(x)
X1 X2 X3
1 1 1 1
2 1 1 1
3 1 1 1
> as.data.frame(x)
V1 V2 V3
1 1 1 1
2 1 1 1
3 1 1 1
推荐答案
如Jaap所述, data.frame()
调用 as.data.frame()
,但这是有原因的:
As mentioned by Jaap, data.frame()
calls as.data.frame()
but there's a reason for it:
as.data.frame()
是一种强制其他对象归类为 data.frame
的方法。如果您正在编写自己的程序包,则将存储您的方法以在 as.data.frame.your_class()<下转换
your_class
对象/ code>。这里只是几个例子。
as.data.frame()
is a method to coerce other objects to class data.frame
. If you're writing your own package, you would store your method to convert an object of your_class
under as.data.frame.your_class()
. Here are just a few examples.
methods(as.data.frame)
[1] as.data.frame.AsIs as.data.frame.Date
[3] as.data.frame.POSIXct as.data.frame.POSIXlt
[5] as.data.frame.aovproj* as.data.frame.array
[7] as.data.frame.character as.data.frame.complex
[9] as.data.frame.data.frame as.data.frame.default
[11] as.data.frame.difftime as.data.frame.factor
[13] as.data.frame.ftable* as.data.frame.integer
[15] as.data.frame.list as.data.frame.logLik*
[17] as.data.frame.logical as.data.frame.matrix
[19] as.data.frame.model.matrix as.data.frame.numeric
[21] as.data.frame.numeric_version as.data.frame.ordered
[23] as.data.frame.raw as.data.frame.table
[25] as.data.frame.ts as.data.frame.vector
Non-visible functions are asterisked
这篇关于as.data.frame(x)和data.frame(x)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文