I remember reading somewhere that as.tibble()
is an alias for as_data_frame()
, but I don't know what exactly an alias is in programming terminology. Is it similar to a wrapper?
So I guess my question probably comes down to the difference in possible usages between tbl_df()
and as_data_frame()
: what are the differences between them, if any?
More specifically, given a (non-tibble) data frame df
, I often turn it into a tibble by using:
df <- tbl_df(df)
Wouldn't
df <- as_data_frame(df)
do the same thing? If so, are there other cases where the two functions tbl_df()
and as_data_frame()
can not be used interchangeably to get the same result?
The R documentation says that
tbl_df()
forwards the argument toas_data_frame()
does that mean that tbl_df()
is a wrapper or alias for as_data_frame()
? R documentation doesn't seem to say anything about as.tibble()
and I forgot where I read that it was an alias for as_data_frame()
. Also, apparently as_tibble()
is another alias for as_data_frame()
.
If these four functions really are all the same function, what is the sense in giving one function four different names? Isn't that more confusing than helpful?
According to the introduction to tibble, it seems like tibbles supersede
tbl_df
.To add to the confusion,
tbl_df
now callsas_tibble
, which is the preferred alias foras_data_frame
andas.tibble
: (Hadley Wickham's comment on the issue, and as_tibble docs)According to the help description of
tbl_df()
, it is deprecated andtibble::as_tibble()
should be used instead.as_data_frame
andas.tibble
help pages both redirect toas_tibble
.When calling
class
on a tibble, the class name still shows up astbl_df
:To answer your question of "whether it is confusing", I think so :) .
as.tibble
andas_tibble
are the same; both simply call the S3 methodas_tibble
:as_data_frame
andtbl_df
are not exactly the same;tbl_df
callsas_data_frame
:Note
tbl_df
is indplyr
whileas_data_frame
is in thetibble
package:but of course it calls the same function, so they are "the same", or aliases as you say.
Now, we can look at the differences between the generic methods
as_tibble
andas_data_frame
. First, we look at the methods of each:If you check out the code for
as_tibble
, you can see that the definitions for many of theas_data_frame
methods as well.as_tibble
defines two additional methods which aren't defined foras_data_frame
,as_tibble.ts
andas_tibble.poly
. I'm not really sure why they couldn't be also defined foras_data_frame
.as_data_frame
has two additional methods, which are both defined indplyr
:as_data_frame.tbl_cube
andas_data_frame.grouped_df
.as_data_frame.tbl_cube
use the weaker checking ofas.data.frame
(yes, bear with me) to then callas_data_frame
:while
as_data_frame.grouped_df
ungroups the passed dataframe.Overall, it seems that
as_data_frame
should be seen as providing additional functionality overas_tibble
, unless you are dealing withts
orpoly
objects.