data.frame 和 data.table 的 R 对象具有相同的类型? [英] R object of data.frame and data.table have same type?

查看:16
本文介绍了data.frame 和 data.table 的 R 对象具有相同的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 R 还是很陌生,最近遇到了一些我不确定它是什么意思的东西.data.framedata.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函数和包only 接受 data.frame.

Since a data.table is a data.frame, it is compatible with R functions and packages that only accept data.frame.

你可以自己测试一下:

library(data.table)
CARS <- data.table(cars)

以下都应该给你相同的结果.它们不是data.table"的做事方式,但我只是突然想到了一些事情来向您展示许多(大多数?)函数可以与 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]

为了更好地理解这一点,我建议阅读常见问题解答.特别是,在这个问题上总结了几个相关点:你可以用 data.frame 做哪些你不能在 data.table 中做的事情.

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 函数.如果你看class,它返回rle",如果你看它的structure,它表明它是一个列表.

Consider the rle function. If you look at the class, it returns "rle", and if you look at its structure, 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

或者,让我们向对象添加另一个 class 并尝试:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆