r - tibble and dataframe, function
问题描述
mat_iris <- as.matrix(iris)
tidyr::as_tibble(mat_iris)
tibble::as_tibble(mat_iris)
tidyr background work tibble ?
dplyr::tbl_df(mat_iris)
dplyr::as_data_frame(mat_iris)
What's the difference two ?
https://www.rdocumentation.org/packages/dplyr/versions/0.5.0/topics/tbl_df https://www.rdocumentation.org/packages/tibble/versions/1.0/topics/as_data_frame
解决方案
I think some of your confusion is about the difference between tibbles and data.frames. Tibbles are a special class of data.frame. Tibbles add increased functionality to data.frames, like improved printing by providing the data type in each column when printed. Tibbles also default to printing only the first ten rows, and make some adjustments based on the width of the output console.
Compare what happens in the console when you print mtcars
versus tibble(mtcars)
.
Another difference is that tibbles are stricter when you ask them to do something, like subsetting. Compare what happens when you ask mtcars
for a column that does not exist.
# data.frame
mtcars$bounce
NULL
# tibble
tibble(mtcars)$bounce
NULL
Warning message:
Unknown or uninitialised column: `bounce`.
The tibble kindly informs you that you asked for something that does not exist.
One last difference is that tibbles can have columns where the entries are not values, but vectors, lists, and other data.frames.
Read more about the difference at this blog entry.
As for the specific functions: as_tibble()
converts something to a tibble. The functions tibble::as_tibble
, dplyr::as_tibble
and tidyr::as_tibble
are all the same function. The function is from the tibble
package, but it has been exported to dplyr
and tidyr
so you can use it in those packages without needing to load tibble
.
The tbl_df()
function from dplyr
is a legacy function for dplyr
to use to coerce things into tibbles. The documentation page for tbl_df()
indicated that this function has been superseded by as_tibble()
. You should use as_tibble()
, but tbl_df()
remains in the package for reverse compatibility.
The as_data_frame()
function is a deprecated function for columnwise construction of tibbles that has been replaced by as_tibble()
. I am using tibble v 3.0.3
and dplyr v 1.0.1
, and I get prevented from trying use as_data_frame()
with a warning message to to use as_tibble()
instead.
推荐阅读
- powershell-core - 为什么 Import-Module 会在 AppData\Local\Temp 中创建 .psd1 和 .psm1 文件?
- geant4 - 为什么我的修改后的 Pol01 在批处理模式下运行适用于除左圆偏振光子之外的所有偏振光子状态?
- api - 如何知道 Netlify 上的部署是否通过 API 完成?
- c++ - 试图弄清楚 cmath.h 如何计算 Trancedental 函数的值。但是函数调用在头文件中似乎是递归的
- apache-nifi - 如何避免 NiFi 中的流文件失败记录?
- .htaccess - .htaccess 子文件夹来读取父文件夹文件,同时保持“漂亮”的 url
- python - 如何改变图像中的噪点颜色?
- java - Gradle 同步失败:找不到 com.android.tools.build:gradle:4.0.1
- python-3.x - 使用shogun在python中实现多任务多核学习
- python - 如何在 Tkinter 中重置/延迟 .after?