Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cbind with data table and data frame

I have a question regarding the characteristics of cbind in a data table and a data frame. If I am binding a data frame(df) and a data table(dt), then the class of the resultant object depends on the first parameter.

example:

> dt<-data.table(x=c(1,2,3),y=c(2,3,4),z=c(3,4,5))
> df<-data.frame(x=c(1,2,3),y=c(2,3,4),z=c(3,4,5))
> dt
   x y z
1: 1 2 3
2: 2 3 4
3: 3 4 5

Case 1:(first parameter as data frame)

>test_df<-cbind(df,dt)
>class(test_df)
[1] "data.frame"

Case 2:(first parameter as data table)

>test_dt<-cbind(dt,df)
>class(test_dt)
[1] "data.table" "data.frame"

So, my question is how is it possible to get the output object as a data table and data frame in two different scenarios, where cbind doesn't have a data.table method.

It is clear in the merge function where R calls different merge functions depending up on the first parameter (if first parameter is a data frame it calls, data frame and if it is a data table, it calls the data table method of merge).

like image 898
Anuj Avatar asked Dec 01 '15 07:12

Anuj


People also ask

What is the difference between Cbind and data frame?

frame() function works very similarly to cbind() – the only difference is that in data. frame() you specify names to each of the columns as you define them. Again, unlike matrices, dataframes can contain both string vectors and numeric vectors within the same object.

Does Cbind create Dataframe?

The functions cbind and rbind are S3 generic, with methods for data frames. The data frame method will be used if at least one argument is a data frame and the rest are vectors or matrices.

What is the use of Rbind () and Cbind () in R explain with example?

rbind(): combining vectors or lists with equal number of columns. All columns must be of the same data type. For example, character columns cannot be combined with numeric columns. rbind() is the equivalent of stacking data sets on top of each other.

What is the function of Cbind () in R programming?

cbind() function in R Language is used to combine specified Vector, Matrix or Data Frame by columns.


1 Answers

If you look at the code of cbind.data.frame you see that there's a check for the data.table case:

cbind.data.frame
# function (..., deparse.level = 1) 
# {
#     if (!identical(class(..1), "data.frame")) 
#         for (x in list(...)) {
#             if (inherits(x, "data.table")) 
#                 return(data.table::data.table(...))
#         }
#     data.frame(..., check.names = FALSE)
# }
# <environment: namespace:base>

Rather odd, I'd agree - I would have expected a cbind.data.table method, but I guess there were some good reasons for not doing it.

like image 141
thothal Avatar answered Sep 30 '22 07:09

thothal