data.frame和data.table的R对象是否具有相同的类型? [英] R object of data.frame and data.table have same type?
问题描述
我对R还是很陌生,最近遇到了不确定的含义。 data.frame
和 data.table
是否具有相同的类型?一个对象可以有多种类型吗?在将汽车从 data.frame
转换为 data.table
之后,我显然无法应用适用于 data.frames
而不是 data.table
,而不是 class()
显示汽车仍然是 data.frame
。有人知道为什么吗?
I am still very new to R and recently came across something I am not sure what it means. data.frame
and data.table
have same type? Can an object have multiple types? After converting "cars" from data.frame
to data.table
, I obviously can't apply functions that apply to data.frames
and not data.table
, but class()
shows the "cars" is still a data.frame
. Anyone know why?
> class(cars)
[1] "data.frame"
> cars<-data.table(cars)
> class(cars)
[1] "data.table" "data.frame"
推荐答案
目前尚不清楚您所说的我显然不能应用适用于data.frames而不适用于data.table的函数这一行的意思。
It is not clear what you mean by your line "I obviously can't apply functions that apply to data.frames and not data.table".
无论应用于 data.frame
还是 data.table
。特别是,如果您在?data.table
中阅读了帮助页面,则会在说明的第一段中找到以下特定行:
Many functions work as you would expect, whether applied to a data.frame
or to a data.table
. In particular, if you read the help page to ?data.table
, you would find this specific line in the first paragraph of the description:
由于
data.table
是是data.frame
,它与R函数和仅兼容接受
data.frame
的程序包兼容。
Since a
data.table
is adata.frame
, it is compatible with R functions and packages thatonly
acceptdata.frame
.
您可以自己进行测试:
library(data.table)
CARS <- data.table(cars)
以下内容应会为您提供相同的结果。它们不是 data.table的处理方式,但是我只是从脑海中弹出了一些东西,向您展示了许多(大多数?)函数可以与 data.table
与将它们与 data.frame
一起使用的方式相同(但是到那时,您会错过所有出色的东西 data.table
必须提供)。
The following should all give you the same results. They aren't the "data.table" way of doing things, but I've just popped off a few things off the top of my head to show you that many (most?) functions can be used with data.table
the same way that you would use them with data.frame
(but at that point, you miss out on all the great stuff that data.table
has to offer).
with(cars, tapply(dist, speed, FUN = mean))
with(CARS, tapply(dist, speed, FUN = mean))
aggregate(dist ~ speed, cars, as.vector)
aggregate(dist ~ speed, CARS, as.vector)
colSums(cars)
colSums(CARS)
as.matrix(cars)
as.matrix(CARS)
t(cars)
t(CARS)
table(cut(cars$speed, breaks=3), cut(cars$dist, breaks=5))
table(cut(CARS$speed, breaks=3), cut(CARS$dist, breaks=5))
cars[cars$speed == 4, ]
CARS[CARS$speed == 4, ]
但是,在某些情况下,这是行不通的。比较:
However, there are some cases in which this won't work. Compare:
cars[cars$speed == 4, 1]
CARS[CARS$speed == 4, 1]
要对此有更好的理解,建议阅读FAQ。特别是,在此问题上已总结了几个相关的要点:如何处理data.frame中无法处理的内容。
For a better understanding of that, I recommend reading the FAQs. In particular, a couple of relevant points have been summarized at this question: what you can do with data.frame that you can't in data.table.
如果您的问题更笼统地说是一个对象可以具有一个以上的类吗?,那么您从自己的探索中已经看到了,可以。有关此的更多信息,您可以阅读Hadley的devtools Wiki上的此页面。
If your question is, more generally, "Can an object have more than one class?", then you've seen from your own exploration that, yes, it can. For more about that, you can read this page from Hadley's devtools wiki.
类也会影响对象的打印方式以及它们与其他功能的交互方式。
Classes also affect things like how objects are printed and how they interact with other functions.
考虑 rle
函数。如果查看类
,它将返回 rle,如果查看其 str
的内容,则会显示这是一个清单。
Consider the rle
function. If you look at the class
, it returns "rle", and if you look at its str
ucture, it shows that it is a list.
> x <- rev(rep(6:10, 1:5))
> y <- rle(x)
> x
[1] 10 10 10 10 10 9 9 9 9 8 8 8 7 7 6
> y
Run Length Encoding
lengths: int [1:5] 5 4 3 2 1
values : int [1:5] 10 9 8 7 6
> class(y)
[1] "rle"
> str(y)
List of 2
$ lengths: int [1:5] 5 4 3 2 1
$ values : int [1:5] 10 9 8 7 6
- attr(*, "class")= chr "rle"
每个列表的长度项目是相同的,您可能希望可以方便地使用 data.frame()
将其转换为 data.frame
。让我们尝试一下:
As the length of each list item is the same, you might expect that you can conveniently use data.frame()
to convert it to a data.frame
. Let's try:
> data.frame(y)
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) :
cannot coerce class ""rle"" to a data.frame
> unclass(y)
$lengths
[1] 5 4 3 2 1
$values
[1] 10 9 8 7 6
> data.frame(unclass(y))
lengths values
1 5 10
2 4 9
3 3 8
4 2 7
5 1 6
或者,我们再添加一个类
对象并尝试:
Or, let's add another class
to the object and try:
> class(y) <- c(class(y), "list")
> y ## Printing is not affected
Run Length Encoding
lengths: int [1:5] 5 4 3 2 1
values : int [1:5] 10 9 8 7 6
> data.frame(y) ## But interaction with other functions is
lengths values
1 5 10
2 4 9
3 3 8
4 2 7
5 1 6
这篇关于data.frame和data.table的R对象是否具有相同的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!